enrich-listing-context.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import 'dotenv/config';
  2. import { loadConfig } from '../src/config/env.js';
  3. import { ParseRestClient } from '../src/db/parse-rest.client.js';
  4. import { ensureListingParseSchemas, VOC_PARSE_CLASSES } from '../src/db/parse-rest.schema.js';
  5. import { buildListingContextIndex } from '../src/modules/listing-ai/normalization/listing-context.enricher.js';
  6. import { ParseRestListingAiRepository } from '../src/modules/listing-ai/repositories/parse-rest-listing-ai.repository.js';
  7. async function main() {
  8. const config = loadConfig();
  9. if (config.storageDriver !== 'parse_rest') throw new Error('listing_context_enrichment_requires_parse_rest');
  10. const workspaceId = process.env.SAAS_DEFAULT_WORKSPACE_ID ?? config.auth.defaultWorkspaceId;
  11. const client = new ParseRestClient({ serverUrl: config.parse.serverUrl, appId: config.parse.appId, masterKey: config.parse.masterKey, timeoutMs: config.parse.timeoutMs });
  12. await ensureListingParseSchemas(client);
  13. const repository = new ParseRestListingAiRepository(client);
  14. const [sources, products, relations, reviews] = await Promise.all([
  15. repository.listAllSources(workspaceId, 'jd'),
  16. client.findAll<Record<string, unknown>>(VOC_PARSE_CLASSES.product, { workspaceId }),
  17. client.findAll<Record<string, unknown>>(VOC_PARSE_CLASSES.productRelation, { workspaceId }),
  18. client.findAll<Record<string, unknown>>(VOC_PARSE_CLASSES.review, { workspaceId }),
  19. ]);
  20. const index = buildListingContextIndex(products, relations, reviews);
  21. let categoryReady = 0; let vocReady = 0;
  22. for (const source of sources) {
  23. const enriched = index.enrich(source);
  24. if (enriched.categoryContext?.ruleVersion) categoryReady += 1;
  25. if (enriched.vocEvidence?.length) vocReady += 1;
  26. await repository.upsertSources([enriched]);
  27. }
  28. console.log(JSON.stringify({ workspaceId, sources: sources.length, enriched: sources.length, categoryReady, vocReady }, null, 2));
  29. }
  30. main().catch((error) => { console.error(`[enrich-listing-context] ${error instanceof Error ? error.message : error}`); process.exitCode = 1; });