MarcoLeung052 commited on
Commit
f03486f
·
verified ·
1 Parent(s): 56eec90

Update backend/fixed_output.py

Browse files
Files changed (1) hide show
  1. backend/fixed_output.py +52 -8
backend/fixed_output.py CHANGED
@@ -2,20 +2,64 @@
2
 
3
  import os
4
 
5
- BASE_DIR = os.path.dirname(os.path.abspath(__file__))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def run_fixed_output(field_name: str):
8
  """
9
  讀取 backend/skills/<field_name>_fixed/<field_name>.md
10
- 回傳純文字內容(先不解析,之後你要可以再加 parser)
11
  """
12
- folder = os.path.join(BASE_DIR, "skills", f"{field_name}_fixed")
13
- file_path = os.path.join(folder, f"{field_name}.md")
14
 
15
- if not os.path.exists(file_path):
16
- return f"[Error] 找不到固定輸出檔案:{file_path}"
17
 
18
- with open(file_path, "r", encoding="utf-8") as f:
19
  content = f.read().strip()
20
 
21
- return content
 
 
2
 
3
  import os
4
 
5
+ BASE = os.path.dirname(os.path.abspath(__file__))
6
+
7
+ def parse_fixed_md(content: str):
8
+ """
9
+ 將固定 skill 的 .md 格式解析成 JSON 結構:
10
+ [STEP]
11
+ 標題
12
+ - 選項
13
+ - 選項
14
+ """
15
+ lines = [line.strip() for line in content.split("\n") if line.strip()]
16
+ steps = []
17
+ current_step = None
18
+
19
+ i = 0
20
+ while i < len(lines):
21
+ line = lines[i]
22
+
23
+ # 遇到 [STEP] → 開始新段落
24
+ if line == "[STEP]":
25
+ if current_step:
26
+ steps.append(current_step)
27
+
28
+ # 下一行是標題
29
+ i += 1
30
+ if i < len(lines):
31
+ label = lines[i]
32
+ else:
33
+ label = ""
34
+
35
+ current_step = {"label": label, "options": []}
36
+
37
+ # 選項行:以 "-" 開頭
38
+ elif line.startswith("-") and current_step:
39
+ option = line.lstrip("-").strip()
40
+ current_step["options"].append(option)
41
+
42
+ i += 1
43
+
44
+ # 最後一段加入
45
+ if current_step:
46
+ steps.append(current_step)
47
+
48
+ return steps
49
+
50
 
51
  def run_fixed_output(field_name: str):
52
  """
53
  讀取 backend/skills/<field_name>_fixed/<field_name>.md
54
+ 解析 JSON 結構
55
  """
56
+ path = os.path.join(BASE, "skills", f"{field_name}_fixed", f"{field_name}.md")
 
57
 
58
+ if not os.path.exists(path):
59
+ return f"[Error] 找不到固定輸出:{path}"
60
 
61
+ with open(path, "r", encoding="utf-8") as f:
62
  content = f.read().strip()
63
 
64
+ # ⭐ 回傳解析後的 JSON,而不是純文字
65
+ return parse_fixed_md(content)