File size: 7,423 Bytes
4d4e5d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from pathlib import Path

path = Path("app/product/final_product_ui.py")
text = path.read_text(encoding="utf-8-sig")
text = text.replace("\ufeff", "")

start = text.find("function renderAnswerHtml(question, data, doc) {")
if start == -1:
    raise RuntimeError("renderAnswerHtml function not found.")

end = text.find("\n\nasync function sendMessage()", start)
if end == -1:
    raise RuntimeError("Could not find end of renderAnswerHtml function.")

new_block = r'''
function getSelectedAnswerStyle() {
    const el = document.getElementById("answerStyle");
    return el ? el.value : "detailed";
}

function cleanMainAnswerText(answer) {
    let text = String(answer || "").trim();

    text = text.replace(/\[S\d+\]/g, "");
    text = text.replace(/Vectorless_RAG_Master_Guide\.pdf/gi, "");
    text = text.replace(/Evidence used[\s\S]*$/i, "");
    text = text.replace(/Sources used[\s\S]*$/i, "");
    text = text.replace(/Source\s+\d+[\s\S]*$/i, "");
    text = text.replace(/[ \t]+/g, " ");
    text = text.replace(/\n{3,}/g, "\n\n");

    return text.trim();
}

function looksLikeRawChunkDump(text) {
    const lower = String(text || "").toLowerCase();

    const rawSignals = [
        "chunk_id",
        "document_id",
        "entity_id",
        "source_path",
        "class document",
        "attributes document",
        "this document contains this chunk",
        "this chunk belongs",
        "page 25 of",
        "page 32 of"
    ];

    return rawSignals.some(x => lower.includes(x));
}

function countWords(text) {
    return String(text || "").split(/\s+/).filter(Boolean).length;
}

function cleanSourcePreviewForAnswer(text) {
    let value = String(text || "").trim();

    value = value.replace(/chunk_id[:\s]+[A-Za-z0-9_\-]+/gi, "");
    value = value.replace(/document_id[:\s]+[A-Za-z0-9_\-]+/gi, "");
    value = value.replace(/entity_id[:\s]+[A-Za-z0-9_\-]+/gi, "");
    value = value.replace(/source_path[:\s]+[^\n]+/gi, "");
    value = value.replace(/\s+/g, " ");
    value = value.replace(/Page\s+\d+\s+of\s+\d+/gi, "");

    return value.trim();
}

function collectSourceSentences(data, doc) {
    const sources = buildSources(data, doc);
    const sentences = [];
    const seen = new Set();

    sources.forEach(src => {
        const preview = cleanSourcePreviewForAnswer(src.preview);

        preview
            .split(/(?<=[.!?])\s+/)
            .map(x => x.trim())
            .filter(x => x.length > 35 && x.length < 260)
            .forEach(sentence => {
                const key = sentence.toLowerCase();
                if (!seen.has(key)) {
                    seen.add(key);
                    sentences.push(sentence);
                }
            });
    });

    return sentences.slice(0, 10);
}

function buildExpandedAnswerFromSources(question, rawAnswer, data, doc, style) {
    const cleanAnswer = cleanMainAnswerText(rawAnswer);
    const sourceSentences = collectSourceSentences(data, doc);
    const questionLower = String(question || "").toLowerCase();

    const wantsSteps =
        questionLower.includes("step") ||
        questionLower.includes("build") ||
        questionLower.includes("procedure") ||
        questionLower.includes("sequential") ||
        style === "step_by_step";

    let basePoints = [];

    if (cleanAnswer && !looksLikeRawChunkDump(cleanAnswer)) {
        basePoints = cleanAnswer
            .split(/(?<=[.!?])\s+/)
            .map(x => x.trim())
            .filter(x => x.length > 25);
    }

    const allPoints = [...basePoints, ...sourceSentences]
        .map(x => x.trim())
        .filter(Boolean);

    const unique = [];
    const seen = new Set();

    allPoints.forEach(point => {
        const key = point.toLowerCase().slice(0, 120);
        if (!seen.has(key)) {
            seen.add(key);
            unique.push(point);
        }
    });

    if (!unique.length) {
        return {
            title: "Answer",
            blocks: ["I found related chunks, but the answer was too weak to expand cleanly. Please re-index the document and ask again."],
            ordered: false
        };
    }

    if (style === "concise") {
        return {
            title: "Concise answer",
            blocks: unique.slice(0, 3),
            ordered: false
        };
    }

    if (style === "research") {
        return {
            title: "Research-style answer",
            blocks: [
                "Overview: " + unique[0],
                "Key details: " + unique.slice(1, 4).join(" "),
                "Interpretation: The document connects these ideas as part of the system design and implementation flow."
            ].filter(x => x.length > 20),
            ordered: false
        };
    }

    if (wantsSteps) {
        return {
            title: "Step-by-step answer",
            blocks: unique.slice(0, 8),
            ordered: true
        };
    }

    return {
        title: "Detailed answer",
        blocks: unique.slice(0, 7),
        ordered: false
    };
}

function renderBlocksAsHtml(blocks, ordered) {
    if (!blocks || !blocks.length) return "<p>No answer generated.</p>";

    if (ordered) {
        let html = "<ol>";
        blocks.forEach(block => {
            html += `<li>${escapeHtml(block.replace(/^\d+\.\s+/, ""))}</li>`;
        });
        html += "</ol>";
        return html;
    }

    if (blocks.length >= 3) {
        let html = "<ul>";
        blocks.forEach(block => {
            html += `<li>${escapeHtml(block)}</li>`;
        });
        html += "</ul>";
        return html;
    }

    return blocks.map(block => `<p>${escapeHtml(block)}</p>`).join("");
}

function renderAnswerHtml(question, data, doc) {
    const style = getSelectedAnswerStyle();
    const rawAnswer = String(data.answer || "I could not generate an answer.").trim();

    if (rawAnswer.toLowerCase().includes("i could not find relevant indexed sources")) {
        return `<div class="answer-card">
            <h2>I could not find indexed evidence</h2>
            <p>The backend does not currently have indexed chunks for this document.</p>
            <p>Use <b>Clear Workspace Cache</b>, upload the document again, then ask once more.</p>
        </div>`;
    }

    const cleaned = cleanMainAnswerText(rawAnswer);
    const shouldExpand =
        countWords(cleaned) < 140 ||
        looksLikeRawChunkDump(cleaned) ||
        style === "step_by_step" ||
        style === "research";

    let finalAnswer;

    if (shouldExpand) {
        finalAnswer = buildExpandedAnswerFromSources(question, rawAnswer, data, doc, style);
    } else {
        finalAnswer = {
            title:
                style === "concise" ? "Concise answer" :
                style === "research" ? "Research-style answer" :
                style === "step_by_step" ? "Step-by-step answer" :
                "Detailed answer",
            blocks: cleaned
                .split(/\n+/)
                .map(x => x.trim())
                .filter(Boolean),
            ordered: style === "step_by_step"
        };
    }

    let html = `<div class="answer-card">`;
    html += `<h2>${escapeHtml(finalAnswer.title)}</h2>`;
    html += renderBlocksAsHtml(finalAnswer.blocks, finalAnswer.ordered);
    html += `</div>`;

    return html;
}
'''

text = text[:start] + new_block.strip() + text[end:]

path.write_text(text, encoding="utf-8")
print("Phase 41 applied: Answer Style now affects final output.")