-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.js
More file actions
68 lines (63 loc) · 2.92 KB
/
Copy pathparser.js
File metadata and controls
68 lines (63 loc) · 2.92 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
export function parsePlanningOutput(rawText) {
try {
const jsonMatch = rawText.match(/\{[\s\S]*\}/);
if (!jsonMatch) {
console.warn("[NA] parsePlanningOutput: \u672a\u627e\u5230JSON, rawText\u524d100\u5b57:", rawText?.substring(0, 100));
return getDefaultPlan();
}
const parsed = JSON.parse(jsonMatch[0]);
return {
narrative_direction: parsed.narrative_direction || "",
key_points: Array.isArray(parsed.key_points) ? parsed.key_points : [],
tone: parsed.tone || "\u4e2d",
pacing: ["\u5feb", "\u4e2d", "\u6162"].includes(parsed.pacing) ? parsed.pacing : "\u4e2d",
continuity_notes: Array.isArray(parsed.continuity_notes) ? parsed.continuity_notes : [],
tool_calls: Array.isArray(parsed.tool_calls) ? parsed.tool_calls : [],
text_recall: Array.isArray(parsed.text_recall) ? parsed.text_recall.filter(v => typeof v === "number" && v >= 0) : [],
};
} catch (e) {
console.warn("[NA] parsePlanningOutput: JSON\u89e3\u6790\u5931\u8d25,", e.message, "rawText\u524d100\u5b57:", rawText?.substring(0, 100));
return getDefaultPlan();
}
}
export function getDefaultPlan() {
return { narrative_direction: "", key_points: [], tone: "\u4e2d", pacing: "\u4e2d", continuity_notes: [], tool_calls: [], text_recall: [] };
}
export function parseExtractionOutput(rawText) {
if (!rawText || typeof rawText !== "string") return { events: [] };
let text = rawText.trim();
const fenceMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/);
if (fenceMatch) text = fenceMatch[1].trim();
try {
const jsonMatch = text.match(/\{[\s\S]*\}/);
if (!jsonMatch) return { events: [] };
const parsed = JSON.parse(jsonMatch[0]);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return { events: [] };
if (!Array.isArray(parsed.events)) return { events: [] };
return { events: parsed.events.filter(ev => ev && typeof ev === "object" && typeof ev.type === "string") };
} catch { return { events: [] }; }
}
export function parseMergedOutput(rawText) {
const result = { events: [], summary_entries: [] };
if (!rawText || typeof rawText !== "string") return result;
let text = rawText.trim();
const fenceMatch = text.match(/```(?:json)?\s*([\s\S]*?)```/);
if (fenceMatch) text = fenceMatch[1].trim();
try {
const jsonMatch = text.match(/\{[\s\S]*\}/);
if (!jsonMatch) {
console.warn("[NA] parseMergedOutput: \u672a\u627e\u5230JSON");
return result;
}
const parsed = JSON.parse(jsonMatch[0]);
if (Array.isArray(parsed.events)) {
result.events = parsed.events.filter(ev => ev && typeof ev === "object" && typeof ev.type === "string");
}
if (Array.isArray(parsed.summary_entries)) {
result.summary_entries = parsed.summary_entries.filter(s => typeof s === "string" && s.trim().length > 0);
}
} catch (e) {
console.warn("[NA] parseMergedOutput: JSON\u89e3\u6790\u5931\u8d25", e.message);
}
return result;
}