extract-store-mock-jsonContent.cjs 1.89 KB
/**
 * 从 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()