-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchild-questions.ts
More file actions
138 lines (114 loc) · 4.09 KB
/
child-questions.ts
File metadata and controls
138 lines (114 loc) · 4.09 KB
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
export type ChildQuestionsDetection = {
hasQuestions: boolean;
questionsText: string | null;
source: "section" | "heuristic" | null;
};
export function detectChildQuestions(text: string): ChildQuestionsDetection {
const normalized = normalizeText(text);
const lines = normalized.split("\n");
const section = extractQuestionsSection(lines);
if (section !== null) {
const cleaned = trimBlankEdges(section).join("\n").trim();
if (cleaned.length === 0) return { hasQuestions: false, questionsText: null, source: null };
return { hasQuestions: true, questionsText: cleaned, source: "section" };
}
const heuristic = extractHeuristicQuestionLines(lines);
if (heuristic !== null) {
const cleaned = trimBlankEdges(heuristic).join("\n").trim();
if (cleaned.length === 0) return { hasQuestions: false, questionsText: null, source: null };
return { hasQuestions: true, questionsText: cleaned, source: "heuristic" };
}
return { hasQuestions: false, questionsText: null, source: null };
}
function extractQuestionsSection(lines: string[]): string[] | null {
const headerIndex = findQuestionsHeaderLineIndex(lines);
if (headerIndex === null) return null;
const sectionLines: string[] = [];
for (let i = headerIndex + 1; i < lines.length; i += 1) {
const line = lines[i] ?? "";
if (isLikelySectionBoundary(line) && sectionLines.length > 0) break;
if (looksLikeCodeFence(line) && sectionLines.length > 0) break;
sectionLines.push(line);
}
const trimmed = trimBlankEdges(sectionLines);
if (trimmed.length === 0) return null;
return trimmed;
}
function findQuestionsHeaderLineIndex(lines: string[]): number | null {
for (let i = 0; i < lines.length; i += 1) {
const line = lines[i] ?? "";
if (isQuestionsHeader(line)) return i;
}
return null;
}
function extractHeuristicQuestionLines(lines: string[]): string[] | null {
const picked: string[] = [];
let qPrefixCount = 0;
for (const line of lines) {
if (isQPrefixLine(line)) {
picked.push(line);
qPrefixCount += 1;
continue;
}
if (isQuestionBulletLine(line)) {
picked.push(line);
}
}
const questionCount = picked.filter((l) => l.includes("?")).length;
if (qPrefixCount >= 1) return picked;
if (questionCount >= 2) return picked;
return null;
}
function isQuestionsHeader(line: string): boolean {
const normalized = line.trim().toLowerCase();
if (!normalized.length) return false;
const withoutMarkdown = normalized.replace(/^#{1,6}\s+/, "");
const withoutColon = withoutMarkdown.replace(/\s*:\s*$/, "");
const candidates = [
"questions",
"open questions",
"clarifying questions",
"questions for you",
"questions for orchestrator",
"questionnaire",
"missing info",
"unknowns",
];
return candidates.includes(withoutColon);
}
function isLikelySectionBoundary(line: string): boolean {
const trimmed = line.trim();
if (!trimmed.length) return false;
if (/^#{1,6}\s+\S/.test(trimmed)) return true;
if (/^\*\*[^*]+\*\*\s*$/.test(trimmed)) return true;
if (/^[A-Za-z][A-Za-z0-9 /()_-]{0,40}:\s*$/.test(trimmed)) return true;
return false;
}
function looksLikeCodeFence(line: string): boolean {
return /^\s*```/.test(line);
}
function isQPrefixLine(line: string): boolean {
const trimmed = line.trim();
if (!trimmed.length) return false;
if (!/^q\d*\s*[:\-]/i.test(trimmed)) return false;
return trimmed.includes("?");
}
function isQuestionBulletLine(line: string): boolean {
const trimmed = line.trim();
if (!trimmed.length) return false;
if (!trimmed.includes("?")) return false;
if (/^[-*+]\s+\S/.test(trimmed)) return true;
if (/^\d+[.)]\s+\S/.test(trimmed)) return true;
if (/^\(?[a-zA-Z]\)\s+\S/.test(trimmed)) return true;
return false;
}
function trimBlankEdges(lines: string[]): string[] {
let start = 0;
let end = lines.length;
while (start < end && !(lines[start] ?? "").trim().length) start += 1;
while (end > start && !(lines[end - 1] ?? "").trim().length) end -= 1;
return lines.slice(start, end);
}
function normalizeText(text: string): string {
return text.replace(/\r\n?/g, "\n");
}