File size: 1,734 Bytes
092fd7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import re

from app.storage.models import ConversationRecord, VisualAssetRecord


def resolve_asset(text: str, conversation: ConversationRecord, assets: list[VisualAssetRecord], current_turn_assets: list[VisualAssetRecord]) -> VisualAssetRecord | None:
    if current_turn_assets and re.search(r"\b(this|current)\s+image\b", text, re.I):
        return current_turn_assets[-1]
    ordinal = re.search(r"\b(first|second|third|1st|2nd|3rd)\s+image\b", text, re.I)
    if ordinal:
        value = ordinal.group(1).lower()
        index = {"first": 0, "1st": 0, "second": 1, "2nd": 1, "third": 2, "3rd": 2}[value]
        return assets[index] if len(assets) > index else None
    if re.search(r"\b(previous)\s+image\b", text, re.I) and conversation.active_asset_id:
        ordered = assets
        for idx, asset in enumerate(ordered):
            if asset.id == conversation.active_asset_id and idx > 0:
                return ordered[idx - 1]
    if conversation.last_referenced_asset_id:
        return next((a for a in assets if a.id == conversation.last_referenced_asset_id), None)
    if conversation.active_asset_id:
        return next((a for a in assets if a.id == conversation.active_asset_id), None)
    return assets[-1] if assets else None


def property_view_for_phrase(text: str, default_views: list[str]) -> str | None:
    lowered = text.lower()
    if "those colors" in lowered or "style" in lowered:
        return "presentation_theme"
    if "that chart" in lowered or "data" in lowered or "q3" in lowered:
        return "chart_ir"
    if "total" in lowered or "receipt" in lowered or "text" in lowered:
        return "ocr"
    return default_views[0] if default_views else None