import 'dotenv/config'; import { loadConfig } from '../src/config/env.js'; import { ParseRestClient } from '../src/db/parse-rest.client.js'; import { ensureListingParseSchemas, VOC_PARSE_CLASSES } from '../src/db/parse-rest.schema.js'; import { buildListingContextIndex } from '../src/modules/listing-ai/normalization/listing-context.enricher.js'; import { ParseRestListingAiRepository } from '../src/modules/listing-ai/repositories/parse-rest-listing-ai.repository.js'; async function main() { const config = loadConfig(); if (config.storageDriver !== 'parse_rest') throw new Error('listing_context_enrichment_requires_parse_rest'); const workspaceId = process.env.SAAS_DEFAULT_WORKSPACE_ID ?? config.auth.defaultWorkspaceId; const client = new ParseRestClient({ serverUrl: config.parse.serverUrl, appId: config.parse.appId, masterKey: config.parse.masterKey, timeoutMs: config.parse.timeoutMs }); await ensureListingParseSchemas(client); const repository = new ParseRestListingAiRepository(client); const [sources, products, relations, reviews] = await Promise.all([ repository.listAllSources(workspaceId, 'jd'), client.findAll>(VOC_PARSE_CLASSES.product, { workspaceId }), client.findAll>(VOC_PARSE_CLASSES.productRelation, { workspaceId }), client.findAll>(VOC_PARSE_CLASSES.review, { workspaceId }), ]); const index = buildListingContextIndex(products, relations, reviews); let categoryReady = 0; let vocReady = 0; for (const source of sources) { const enriched = index.enrich(source); if (enriched.categoryContext?.ruleVersion) categoryReady += 1; if (enriched.vocEvidence?.length) vocReady += 1; await repository.upsertSources([enriched]); } console.log(JSON.stringify({ workspaceId, sources: sources.length, enriched: sources.length, categoryReady, vocReady }, null, 2)); } main().catch((error) => { console.error(`[enrich-listing-context] ${error instanceof Error ? error.message : error}`); process.exitCode = 1; });