File size: 22,277 Bytes
06671aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import type { ChatMessage, DocIndex } from "./chatTools";
import type { RunTree } from "langsmith";
import type { UserApiKeys } from "./llm/types";
import {
    buildSaudiLawRoutingResult,
    classifySaudiLawNeed,
    type SaudiLawRoutingResult,
} from "./saudiLawRouting";
import { classifyWebSearchNeed, detectWebSearchIntentHeuristic } from "./webSearchRouting";

/**
 * Explicit assistant-mode contract. Every chat request resolves to exactly
 * one of these. `auto` means "the server may infer a mode from heuristics" —
 * every other value is an explicit user choice that is already authorized by
 * the user's selection and must never be silently upgraded to a mutating mode.
 */
export type RequestedAssistantMode =
    | "auto"
    | "answer"
    | "comments"
    | "document_edit"
    | "workflow_review"
    | "document_generation"
    | "web_search";

const REQUESTED_ASSISTANT_MODES: readonly RequestedAssistantMode[] = [
    "auto",
    "answer",
    "comments",
    "document_edit",
    "workflow_review",
    "document_generation",
    "web_search",
];

/**
 * Validate an untrusted `requested_mode` request field. Throws a tagged error
 * whose message contains `invalid_requested_mode` so routes can translate it to
 * an HTTP 400 before any model/provider/tool/storage/database side effect.
 */
export function parseRequestedAssistantMode(
    value: unknown,
): RequestedAssistantMode {
    if (value === undefined || value === null || value === "") return "auto";
    if (
        typeof value === "string" &&
        (REQUESTED_ASSISTANT_MODES as readonly string[]).includes(value)
    ) {
        return value as RequestedAssistantMode;
    }
    throw new Error(`invalid_requested_mode: ${String(value)}`);
}

export type AssistantTurnMode =
    | "answer"
    | "comments"
    | "document_direct_edit"
    | "document_edit_with_web_search"
    | "web_search"
    | "workflow_review"
    | "document_generation"
    | "project_chat";

export type DocumentModeDecision = {
    trackChanges: boolean;
    reason:
        | "explicit_request_true"
        | "explicit_comments_only"
        | "auto_docx_edit_intent"
        | "no_editable_docx"
        | "default_comments_only"
        | "explicit_non_mutating_mode";
    editIntent: boolean;
    commentsOnlyIntent: boolean;
    editableDocxCount: number;
    pdfCount: number;
};

export type TurnModeSource = "explicit" | "inferred";

export type TurnModeReason =
    | "explicit_mode"
    | "explicit_workflow_execution"
    | "user_requested_workflow"
    | "docx_mutation_with_web_search"
    | "direct_docx_mutation"
    | "web_search_intent"
    | "web_search_routing"
    | "document_generation_intent"
    | "normal_answer"
    | "saudi_law_requires_web_search"
    | "web_search_blocked_docs_attached";

export type TurnModeDecision = {
    turnMode: AssistantTurnMode;
    requestedMode: RequestedAssistantMode;
    source: TurnModeSource;
    documentMode: DocumentModeDecision;
    enableGoogleSearch: boolean;
    requiresConfirmation: boolean;
    reason: TurnModeReason;
    saudiLawRouting?: SaudiLawRoutingResult;
    routingContext?: {
        followUpDetected: boolean;
        inheritedWebSearch: boolean;
        routingText: string;
        inheritanceSource: "deterministic" | "none";
    };
};

const COMMENTS_ONLY_RE =
    /\b(comments? only|feedback only|review only|marginalia|do not edit|don't edit|no edits|no redlines|without changing|do not change|leave the text unchanged)\b/i;

const EDIT_INTENT_RE =
    /\b(rewrite|redline|red-line|edit|revise|amend|modify|change|replace|insert|add|delete|remove|fix|correct|tracked changes?|make .* favourable|make .* favorable|more favourable|more favorable)\b/i;

const COMMENT_EDIT_INTENT_RE =
    /\b(add|insert|place|leave|make|create)\b.{0,40}\b(comment|note|annotation|margin note)\b/i;

const UPDATE_DOCUMENT_INTENT_RE =
    /\bupdate\b.{0,80}\b(document|docx|file|agreement|contract|clause|section|paragraph|text|wording|draft)\b|\b(document|docx|file|agreement|contract|clause|section|paragraph|text|wording|draft)\b.{0,80}\bupdate\b/i;

const WORKFLOW_REVIEW_RE =
    /\b(run|execute|start|apply|use)\b.{0,80}\b(workflow|playbook|review workflow|buyer playbook|seller playbook|zamil playbook)\b/i;

const DOCUMENT_GENERATION_RE =
    /\b(draft|generate|create|produce|prepare|write)\b.{0,80}\b(document|docx|agreement|contract|letter|memo|notice|template|deed|resolution)\b/i;

const FOLLOW_UP_FRAGMENT_RE =
    /^\s*(for|in|at|near|around|and|also|same(?:\s+for)?|what about|how about|now|today|tomorrow|yesterday|next week|this week|source|sources|cite|citation|citations|verify|fact[- ]?check|in arabic|arabic|translate)\b/i;

const REFERENTIAL_FOLLOW_UP_RE =
    /\b(that|this|it|those|these|same|above|previous|earlier|there|here)\b/i;

const TOPIC_RESET_RE =
    /\b(new topic|different question|unrelated|forget that|instead)\b/i;

const PRIOR_TURN_CONTEXT_CHAR_LIMIT = 600;

export type PriorTurnRoutingState = {
    turnMode?: AssistantTurnMode | string | null;
    enableGoogleSearch?: boolean | null;
    reason?: string | null;
};

export function extractPriorTurnRoutingState(
    content: unknown,
): PriorTurnRoutingState | null {
    if (!Array.isArray(content)) return null;
    const modeEvent = content.find((event): event is Record<string, unknown> => {
        return (
            typeof event === "object" &&
            event !== null &&
            (event as Record<string, unknown>).type === "mode_decision"
        );
    });
    const decision = modeEvent?.decision;
    if (typeof decision !== "object" || decision === null) return null;
    const record = decision as Record<string, unknown>;
    return {
        turnMode:
            typeof record.turnMode === "string" ? record.turnMode : undefined,
        enableGoogleSearch:
            typeof record.enableGoogleSearch === "boolean"
                ? record.enableGoogleSearch
                : undefined,
        reason: typeof record.reason === "string" ? record.reason : undefined,
    };
}

function detectWebSearchIntent(
    lastUserText: string,
    hasAttachedDocs = false,
): boolean {
    return detectWebSearchIntentHeuristic(lastUserText, { hasAttachedDocs });
}

type DocIndexEntry = {
    filename?: string;
    file_type?: string | null;
};

function isDocx(info: DocIndexEntry): boolean {
    const type = info.file_type?.toLowerCase();
    const name = info.filename?.toLowerCase() ?? "";
    return type === "docx" || name.endsWith(".docx");
}

function isPdf(info: DocIndexEntry): boolean {
    const type = info.file_type?.toLowerCase();
    const name = info.filename?.toLowerCase() ?? "";
    return type === "pdf" || name.endsWith(".pdf");
}

function latestUserText(messages: ChatMessage[]): string {
    const lastUser = [...messages].reverse().find((m) => m.role === "user");
    if (!lastUser?.content) return "";
    return String(lastUser.content);
}

function previousUserText(messages: ChatMessage[]): string {
    let seenLatest = false;
    for (let i = messages.length - 1; i >= 0; i--) {
        const message = messages[i];
        if (message.role !== "user") continue;
        if (!seenLatest) {
            seenLatest = true;
            continue;
        }
        return String(message.content ?? "");
    }
    return "";
}

function priorStateNeedsWebSearch(
    prior: PriorTurnRoutingState | null | undefined,
): boolean {
    if (!prior) return false;
    if (prior.enableGoogleSearch === true) return true;
    return (
        prior.turnMode === "web_search" ||
        prior.turnMode === "document_edit_with_web_search"
    );
}

function isLikelyFollowUpFragment(text: string): boolean {
    const trimmed = text.trim();
    if (!trimmed) return false;
    if (TOPIC_RESET_RE.test(trimmed)) return false;
    if (WORKFLOW_REVIEW_RE.test(trimmed) || DOCUMENT_GENERATION_RE.test(trimmed)) {
        return false;
    }
    if (EDIT_INTENT_RE.test(trimmed) || COMMENT_EDIT_INTENT_RE.test(trimmed)) {
        return false;
    }
    if (FOLLOW_UP_FRAGMENT_RE.test(trimmed)) return true;
    if (REFERENTIAL_FOLLOW_UP_RE.test(trimmed) && trimmed.length <= 140) {
        return true;
    }
    if (
        trimmed.length <= 32 &&
        !/\b(what|who|why|when|where|how|draft|generate|rewrite|review|explain|tell|define)\b/i.test(
            trimmed,
        )
    ) {
        return true;
    }
    return false;
}

function buildFollowUpRoutingContext(params: {
    messages: ChatMessage[];
    priorTurn?: PriorTurnRoutingState | null;
}): {
    followUpDetected: boolean;
    inheritedWebSearch: boolean;
    routingText: string;
    inheritanceSource: "deterministic" | "none";
} {
    const latest = latestUserText(params.messages).trim();
    const previous = previousUserText(params.messages).trim();
    if (!latest || !previous || !isLikelyFollowUpFragment(latest)) {
        return {
            followUpDetected: false,
            inheritedWebSearch: false,
            routingText: latest,
            inheritanceSource: "none",
        };
    }

    const previousNeededWeb =
        priorStateNeedsWebSearch(params.priorTurn) ||
        detectWebSearchIntent(previous);
    const routingText = `${previous.slice(0, PRIOR_TURN_CONTEXT_CHAR_LIMIT)}\nFollow-up: ${latest}`;

    return {
        followUpDetected: true,
        inheritedWebSearch: previousNeededWeb,
        routingText,
        inheritanceSource: previousNeededWeb ? "deterministic" : "none",
    };
}

function latestUserHasWorkflow(messages: ChatMessage[]): boolean {
    const lastUser = [...messages].reverse().find((m) => m.role === "user");
    return !!lastUser?.workflow?.id;
}

export function resolveDocumentMode(params: {
    requestedTrackChanges?: boolean;
    messages: ChatMessage[];
    docIndex: DocIndex;
}): DocumentModeDecision {
    const lastUserText = latestUserText(params.messages);
    const docs = Object.values(params.docIndex);
    const editableDocxCount = docs.filter(isDocx).length;
    const pdfCount = docs.filter(isPdf).length;
    const hasEditableDocx = editableDocxCount > 0;

    const commentsOnlyIntent = COMMENTS_ONLY_RE.test(lastUserText);
    const editIntent =
        EDIT_INTENT_RE.test(lastUserText) ||
        COMMENT_EDIT_INTENT_RE.test(lastUserText) ||
        UPDATE_DOCUMENT_INTENT_RE.test(lastUserText);

    if (
        params.requestedTrackChanges === true &&
        hasEditableDocx &&
        !commentsOnlyIntent
    ) {
        return {
            trackChanges: true,
            reason: "explicit_request_true",
            editIntent,
            commentsOnlyIntent,
            editableDocxCount,
            pdfCount,
        };
    }

    if (commentsOnlyIntent) {
        return {
            trackChanges: false,
            reason: "explicit_comments_only",
            editIntent,
            commentsOnlyIntent,
            editableDocxCount,
            pdfCount,
        };
    }

    if (!hasEditableDocx) {
        return {
            trackChanges: false,
            reason: "no_editable_docx",
            editIntent,
            commentsOnlyIntent,
            editableDocxCount,
            pdfCount,
        };
    }

    if (editIntent) {
        return {
            trackChanges: true,
            reason: "auto_docx_edit_intent",
            editIntent,
            commentsOnlyIntent,
            editableDocxCount,
            pdfCount,
        };
    }

    return {
        trackChanges: false,
        reason: "default_comments_only",
        editIntent,
        commentsOnlyIntent,
        editableDocxCount,
        pdfCount,
    };
}

function explicitTurnModeFor(
    requestedMode: Exclude<RequestedAssistantMode, "auto">,
): AssistantTurnMode {
    switch (requestedMode) {
        case "answer":
            return "answer";
        case "comments":
            return "comments";
        case "document_edit":
            return "document_direct_edit";
        case "workflow_review":
            return "workflow_review";
        case "document_generation":
            return "document_generation";
        case "web_search":
            return "web_search";
    }
}

function resolveExplicitTurnMode(
    requestedMode: Exclude<RequestedAssistantMode, "auto">,
    params: {
        messages: ChatMessage[];
        docIndex: DocIndex;
        projectId?: string | null;
    },
): TurnModeDecision {
    const allowTrackChanges = requestedMode === "document_edit";
    let documentMode = resolveDocumentMode({
        requestedTrackChanges: allowTrackChanges,
        messages: params.messages,
        docIndex: params.docIndex,
    });

    // Explicit non-mutating modes must never mutate document bytes. Force the
    // document mode flat regardless of what the legacy heuristics inferred so a
    // user who explicitly selected (e.g.) "answer" can never be silently
    // upgraded to tracked-change editing.
    if (!allowTrackChanges) {
        documentMode = {
            ...documentMode,
            trackChanges: false,
            reason: "explicit_non_mutating_mode",
        };
    }

    const lastUserText = latestUserText(params.messages);
    const hasAttachedDocs = Object.keys(params.docIndex).length > 0;
    const wantsWebSearch = detectWebSearchIntent(lastUserText, hasAttachedDocs);

    if (requestedMode === "web_search" && hasAttachedDocs) {
        return {
            turnMode: params.projectId ? "project_chat" : "answer",
            requestedMode,
            source: "explicit",
            documentMode,
            enableGoogleSearch: false,
            requiresConfirmation: false,
            reason: "web_search_blocked_docs_attached",
        };
    }

    return {
        turnMode: explicitTurnModeFor(requestedMode),
        requestedMode,
        source: "explicit",
        documentMode,
        enableGoogleSearch:
            !hasAttachedDocs &&
            (requestedMode === "web_search" ||
                (requestedMode === "answer" && wantsWebSearch)),
        requiresConfirmation: false,
        reason: "explicit_mode",
    };
}

function resolveAutoTurnMode(params: {
    requestedTrackChanges?: boolean;
    messages: ChatMessage[];
    docIndex: DocIndex;
    isExecutingWorkflow?: boolean;
    projectId?: string | null;
}): TurnModeDecision {
    const documentMode = resolveDocumentMode({
        requestedTrackChanges: params.requestedTrackChanges,
        messages: params.messages,
        docIndex: params.docIndex,
    });

    const lastUserText = latestUserText(params.messages);
    const hasWorkflowAttachment = latestUserHasWorkflow(params.messages);
    const hasAttachedDocs = Object.keys(params.docIndex).length > 0;
    const wantsWebSearch = detectWebSearchIntent(lastUserText, hasAttachedDocs);

    const finalize = (
        turnMode: AssistantTurnMode,
        enableGoogleSearch: boolean,
        reason: TurnModeReason,
    ): TurnModeDecision => ({
        turnMode,
        requestedMode: "auto",
        source: "inferred",
        documentMode,
        enableGoogleSearch,
        requiresConfirmation:
            turnMode === "document_direct_edit" ||
            turnMode === "document_edit_with_web_search",
        reason,
    });

    if (params.isExecutingWorkflow || hasWorkflowAttachment) {
        return finalize("workflow_review", false, "explicit_workflow_execution");
    }

    if (WORKFLOW_REVIEW_RE.test(lastUserText)) {
        return finalize("workflow_review", false, "user_requested_workflow");
    }

    if (
        !hasAttachedDocs &&
        documentMode.trackChanges &&
        documentMode.editableDocxCount > 0 &&
        wantsWebSearch
    ) {
        return finalize(
            "document_edit_with_web_search",
            true,
            "docx_mutation_with_web_search",
        );
    }

    if (documentMode.trackChanges && documentMode.editableDocxCount > 0) {
        return finalize("document_direct_edit", false, "direct_docx_mutation");
    }

    if (DOCUMENT_GENERATION_RE.test(lastUserText)) {
        return finalize(
            "document_generation",
            !hasAttachedDocs && wantsWebSearch,
            "document_generation_intent",
        );
    }

    if (!hasAttachedDocs && wantsWebSearch) {
        return finalize("web_search", true, "web_search_intent");
    }

    return finalize(
        params.projectId ? "project_chat" : "answer",
        false,
        "normal_answer",
    );
}

export function resolveTurnMode(params: {
    requestedMode?: RequestedAssistantMode;
    requestedTrackChanges?: boolean;
    messages: ChatMessage[];
    docIndex: DocIndex;
    isExecutingWorkflow?: boolean;
    projectId?: string | null;
}): TurnModeDecision {
    const requestedMode = params.requestedMode ?? "auto";
    if (requestedMode !== "auto") {
        return resolveExplicitTurnMode(requestedMode, {
            messages: params.messages,
            docIndex: params.docIndex,
            projectId: params.projectId,
        });
    }
    return resolveAutoTurnMode(params);
}

export function applySaudiLawRouting(
    decision: TurnModeDecision,
    saudi: SaudiLawRoutingResult,
    requestedMode: RequestedAssistantMode,
): TurnModeDecision {
    if (!saudi.isSaudiLawQuestion) return decision;

    if (
        decision.turnMode === "workflow_review" ||
        decision.requiresConfirmation
    ) {
        return decision;
    }

    const base = {
        ...decision,
        enableGoogleSearch: true,
        saudiLawRouting: saudi,
    };

    if (decision.turnMode === "document_direct_edit") {
        return {
            ...base,
            turnMode: "document_edit_with_web_search",
            reason: "saudi_law_requires_web_search",
        };
    }

    if (requestedMode === "answer") {
        return {
            ...base,
            reason: "saudi_law_requires_web_search",
        };
    }

    if (decision.enableGoogleSearch) {
        return {
            ...base,
            reason: "saudi_law_requires_web_search",
        };
    }

    if (requestedMode === "auto") {
        return {
            ...base,
            turnMode: "web_search",
            reason: "saudi_law_requires_web_search",
        };
    }

    return {
        ...base,
        reason: "saudi_law_requires_web_search",
    };
}

function applyWebSearchRouting(
    decision: TurnModeDecision,
    requestedMode: RequestedAssistantMode,
    routing: Awaited<ReturnType<typeof classifyWebSearchNeed>>,
): TurnModeDecision {
    if (!routing.needsWebSearch || decision.enableGoogleSearch) return decision;

    if (requestedMode === "answer") {
        return {
            ...decision,
            enableGoogleSearch: true,
            reason: routing.source === "llm" ? "web_search_routing" : decision.reason,
        };
    }

    return {
        ...decision,
        turnMode: "web_search",
        enableGoogleSearch: true,
        reason: routing.source === "llm" ? "web_search_routing" : "web_search_intent",
    };
}

export async function resolveTurnModeAsync(params: {
    requestedMode?: RequestedAssistantMode;
    requestedTrackChanges?: boolean;
    messages: ChatMessage[];
    docIndex: DocIndex;
    isExecutingWorkflow?: boolean;
    projectId?: string | null;
    apiKeys?: UserApiKeys;
    routingTrace?: RunTree | null;
    saudiRoutingTrace?: RunTree | null;
    priorTurn?: PriorTurnRoutingState | null;
    /** Eval/test hook — forwarded to classifySaudiLawNeed. */
    saudiClassifierOverride?: (args: {
        userText: string;
        apiKeys?: UserApiKeys;
        trace?: RunTree | null;
    }) => Promise<string>;
}): Promise<TurnModeDecision> {
    const requestedMode = params.requestedMode ?? "auto";
    let decision = resolveTurnMode(params);
    const lastUserText = latestUserText(params.messages);
    const followUpRouting = buildFollowUpRoutingContext({
        messages: params.messages,
        priorTurn: params.priorTurn,
    });
    const routingText = followUpRouting.routingText || lastUserText;
    const hasAttachedDocs = Object.keys(params.docIndex).length > 0;

    if (
        followUpRouting.inheritedWebSearch &&
        !hasAttachedDocs &&
        !decision.enableGoogleSearch &&
        !decision.requiresConfirmation &&
        decision.turnMode !== "workflow_review" &&
        decision.turnMode !== "document_generation"
    ) {
        decision = {
            ...decision,
            turnMode: requestedMode === "auto" ? "web_search" : decision.turnMode,
            enableGoogleSearch: true,
            reason: "web_search_routing",
        };
    }

    decision = {
        ...decision,
        routingContext: followUpRouting,
    };

    const skipGenericRouting =
        decision.enableGoogleSearch ||
        decision.requiresConfirmation ||
        decision.turnMode === "workflow_review" ||
        requestedMode === "web_search";

    const saudiPromise =
        process.env.SAUDI_LAW_ROUTING !== "0"
            ? classifySaudiLawNeed({
                  userText: routingText,
                  apiKeys: params.apiKeys,
                  trace: params.saudiRoutingTrace ?? params.routingTrace,
                  hasAttachedDocs,
                  classifierOverride: params.saudiClassifierOverride,
              })
            : Promise.resolve(
                  buildSaudiLawRoutingResult({
                      isSaudiLawQuestion: false,
                      source: "skip",
                      domain: "unknown",
                      confidence: "high",
                  }),
              );

    const genericPromise = skipGenericRouting
        ? Promise.resolve(null)
        : classifyWebSearchNeed({
              userText: routingText,
              apiKeys: params.apiKeys,
              trace: params.routingTrace,
              hasAttachedDocs,
          });

    const [saudi, routing] = await Promise.all([
        saudiPromise,
        genericPromise,
    ]);

    if (process.env.SAUDI_LAW_ROUTING_LOG === "1") {
        console.log("[saudiLawRouting]", saudi);
    }

    if (saudi.isSaudiLawQuestion) {
        return applySaudiLawRouting(decision, saudi, requestedMode);
    }

    if (skipGenericRouting || routing === null) {
        return decision;
    }

    return applyWebSearchRouting(decision, requestedMode, routing);
}