extract-store-mock-jsonContent.cjs
1.89 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
/**
* 从 agent transcript 提取门店 mock jsonContent 写入 fixture(一次性)
* STORE_MOCK_TRANSCRIPT=<transcript.jsonl> node packageDynamicForm/scripts/extract-store-mock-jsonContent.cjs
*/
const fs = require('fs')
const path = require('path')
const transcriptPath = process.env.STORE_MOCK_TRANSCRIPT
const outPath = path.join(__dirname, '../fixtures/mock-store-detail-jsonContent.json')
const markerStart = 'jsonContent改为如下(['
const markerEnd = '])'
function extractFromLine(line) {
const idx = line.indexOf(markerStart)
if (idx < 0) return null
const start = idx + markerStart.length - 1
let depth = 0
let end = -1
for (let i = start; i < line.length; i++) {
const ch = line[i]
if (ch === '[') depth++
else if (ch === ']') {
depth--
if (depth === 0) {
end = i + 1
break
}
}
}
if (end < 0) return null
return line.slice(start, end)
}
function main() {
if (!transcriptPath) {
console.error('STORE_MOCK_TRANSCRIPT is required')
process.exit(1)
}
if (!fs.existsSync(transcriptPath)) {
console.error('transcript not found:', transcriptPath)
process.exit(1)
}
const lines = fs.readFileSync(transcriptPath, 'utf8').split('\n')
for (const line of lines) {
if (!line.includes(markerStart)) continue
let raw = extractFromLine(line)
if (!raw) continue
// jsonl 行内为 JSON 字符串,需先 parse 外层再取 text
try {
const row = JSON.parse(line)
const text = row?.message?.content?.find(c => c.type === 'text')?.text
if (text) {
raw = extractFromLine(text)
}
} catch {
// line 可能已是纯文本片段
}
if (!raw) continue
const parsed = JSON.parse(raw)
fs.writeFileSync(outPath, JSON.stringify(parsed, null, 2), 'utf8')
console.log('wrote', outPath, 'items', parsed.length)
require('./sync-store-mock-jsonContent-list.cjs')
return
}
console.error('marker not found in transcript')
process.exit(1)
}
main()