dsh-data-cleaning-agent 0.6.3 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/qcc.js CHANGED
@@ -18,6 +18,13 @@ import {
18
18
  QCC_PHASE3_DOMAIN_META,
19
19
  QCC_PHASE3_TOOL_NAMES,
20
20
  } from './qcc-phase3.js';
21
+ import {
22
+ RELATED_RISK_FACTORS,
23
+ RELATED_RISK_KEY_FACTORS,
24
+ RISK_FACTOR_CATALOG_VERSION,
25
+ SELF_RISK_FACTORS,
26
+ selectedSourceTools,
27
+ } from './qcc-field-catalog.js';
21
28
 
22
29
  export const QCC_TOOL_NAMES = Object.freeze({
23
30
  oauthConnect: 'qcc_oauth_connect',
@@ -25,7 +32,12 @@ export const QCC_TOOL_NAMES = Object.freeze({
25
32
  entityLookup: 'mcp__qcc-company__get_company_by_query',
26
33
  registration: 'mcp__qcc-company__get_company_registration_info',
27
34
  profile: 'mcp__qcc-company__get_company_profile',
35
+ contact: 'mcp__qcc-company__get_contact_info',
36
+ listing: 'mcp__qcc-company__get_listing_info',
37
+ taxInvoice: 'mcp__qcc-company__get_tax_invoice_info',
38
+ importExportCredit: 'mcp__qcc-operation__get_import_export_credit',
28
39
  riskScan: 'mcp__qcc-risk__get_company_risk_scan',
40
+ relatedRiskScan: 'mcp__qcc-risk__get_company_related_risk_scan',
29
41
  });
30
42
 
31
43
  const EXACT_MATCH = '唯一精确匹配';
@@ -35,6 +47,7 @@ const DEFAULT_TIMEOUT_MS = 20_000;
35
47
  const DEFAULT_TOOL_WAIT_MS = 1_500;
36
48
  const DEFAULT_POLL_MS = 50;
37
49
  const DEFAULT_MAX_ROWS = 100;
50
+ const DEFAULT_MAX_CALLS = 300;
38
51
  const MAX_CONCURRENCY = 4;
39
52
 
40
53
  function isRecord(value) {
@@ -324,72 +337,244 @@ export function mapRegistrationFields(value, fallback = {}) {
324
337
  }
325
338
  return '';
326
339
  };
327
- const region = text('所属地区', '所属地域', '地区');
328
- const location = splitAdministrativeRegion(region);
329
340
  return {
330
341
  company_name: text('企业名称') || String(fallback.companyName ?? ''),
331
342
  credit_no: text('统一社会信用代码', '信用代码') || String(fallback.creditNo ?? ''),
332
343
  reg_no: text('工商注册号', '注册号'),
333
344
  org_no: text('组织机构代码'),
345
+ tax_no: text('纳税人识别号'),
334
346
  reg_status: text('登记状态', '执业状态', '证书状态'),
335
347
  legal_rep: text('法定代表人', '负责人', '经营者'),
336
348
  reg_capital: text('注册资本', '注册资金', '开办资金', '成员出资总额', '资金数额'),
337
349
  paid_capital: text('实缴资本'),
338
350
  establish_date: text('成立日期'),
339
351
  company_type: text('企业类型', '公司类型'),
352
+ approval_date: text('核准日期'),
340
353
  registration_authority: text('登记机关'),
354
+ taxpayer_qualification: text('纳税人资质'),
355
+ payment_line_no: text('支付系统行号'),
356
+ import_export_company_code: text('进出口企业代码'),
357
+ short_name: text('企业简称'),
341
358
  english_name: text('英文名', '英文名称'),
342
359
  registered_address: text('注册地址', '住所', '经营场所'),
343
- province: text('省份地区', '省份', '所属省份') || location.province,
344
- city: text('城市', '所属城市', '市') || location.city,
345
- district: text('区县', '所属区县') || location.district,
360
+ mailing_address: text('通信地址'),
361
+ region: text('所属地区'),
346
362
  business_scope: text('经营范围'),
347
363
  industry_category: text('国标行业'),
348
- industry_large: text('一级行业', '企查查一级行业'),
349
- industry_middle: text('二级行业', '企查查二级行业'),
350
364
  operating_period: text('营业期限', '经营期限'),
351
- company_size: text('企业规模', '人员规模'),
352
- biz_status: text('经营状态'),
365
+ company_size: text('人员规模'),
366
+ insured_count: text('参保人数'),
367
+ branch_insured_count: text('分支机构参保人数'),
353
368
  };
354
369
  }
355
370
 
356
- function splitAdministrativeRegion(value) {
357
- const region = String(value ?? '').trim().replace(/\s+/g, '');
358
- if (!region) return { province: '', city: '', district: '' };
359
- const municipality = region.match(/^((?:北京|上海|天津|重庆)市)(.*)$/);
360
- if (municipality) {
361
- const district = municipality[2].match(/^(.+?(?:区|县))/)?.[1] ?? '';
362
- return { province: municipality[1], city: municipality[1], district };
363
- }
364
- const provinceMatch = region.match(/^(.+?(?:省|自治区|特别行政区))/);
365
- const province = provinceMatch?.[1] ?? '';
366
- const remainder = province ? region.slice(province.length) : region;
367
- const city = remainder.match(/^(.+?(?:市|自治州|地区|盟))/)?.[1] ?? '';
368
- const districtRemainder = city ? remainder.slice(city.length) : remainder;
369
- const district = districtRemainder.match(/^(.+?(?:区|县|旗|市))/)?.[1] ?? '';
370
- return { province, city, district };
371
- }
372
-
373
371
  export function mapProfileFields(value) {
374
372
  if (!isRecord(value)) {
375
373
  throw new QccBridgeError('QCC_CONTRACT_MISMATCH', 'QCC profile tool returned a non-object result');
376
374
  }
377
375
  if (value.无匹配项 !== undefined) return {};
378
376
  return {
379
- // “企查查行业”没有声明一/二级语义,不得猜测塞入层级列。
380
- industry_large: String(value.一级行业 ?? value.企查查一级行业 ?? ''),
381
- industry_middle: String(value.二级行业 ?? value.企查查二级行业 ?? ''),
377
+ // 工具只返回“企查查行业”这一最细层级展示值,不得猜测为一级或二级行业。
378
+ qcc_industry: String(value.企查查行业 ?? ''),
382
379
  company_profile: String(value.企业简介 ?? value.简介 ?? ''),
380
+ industry_chain_overview: String(value.产业链概览 ?? ''),
381
+ };
382
+ }
383
+
384
+ function scalarText(value, ...keys) {
385
+ if (!isRecord(value)) return '';
386
+ for (const key of keys) {
387
+ const item = value[key];
388
+ if (item !== undefined && item !== null && String(item).trim()) return String(item);
389
+ }
390
+ return '';
391
+ }
392
+
393
+ function scalarMap(value, mapping) {
394
+ if (!isRecord(value) || value.无匹配项 !== undefined || value.地域限制 !== undefined) return {};
395
+ return Object.fromEntries(mapping.map(([id, ...keys]) => [id, scalarText(value, ...keys)]));
396
+ }
397
+
398
+ export function mapContactFields(value) {
399
+ if (!isRecord(value)) return {};
400
+ const contact = isRecord(value.联系方式信息) ? value.联系方式信息 : {};
401
+ const phone = Array.isArray(contact.电话) && isRecord(contact.电话[0]) ? contact.电话[0] : {};
402
+ const email = Array.isArray(contact.邮箱) && isRecord(contact.邮箱[0]) ? contact.邮箱[0] : {};
403
+ const website = Array.isArray(contact.网址)
404
+ ? contact.网址.find((item) => isRecord(item) && item.是否是官网 === '是') ?? {}
405
+ : {};
406
+ return {
407
+ contact_preferred_phone: scalarText(phone, '电话号码'),
408
+ contact_phone_invalid_flag: scalarText(phone, '是否无效'),
409
+ contact_phone_tags: Array.isArray(phone.标签)
410
+ ? phone.标签.map((item) => String(item).trim()).filter(Boolean).join(';')
411
+ : '',
412
+ contact_preferred_email: scalarText(email, '邮箱'),
413
+ contact_official_website: scalarText(website, '网址'),
414
+ contact_official_website_icp: scalarText(website, 'ICP备案'),
415
+ };
416
+ }
417
+
418
+ export function mapListingFields(value) {
419
+ return scalarMap(value, [
420
+ ['listing_date', '上市日期'],
421
+ ['listing_short_name', '股票简称'],
422
+ ['listing_stock_code', '股票代码'],
423
+ ['listing_exchange', '上市交易所'],
424
+ ['listing_board', '上市板块'],
425
+ ['listing_former_short_name', '上市曾用名'],
426
+ ['listing_total_market_value', '总市值'],
427
+ ['listing_total_shares', '总股本'],
428
+ ['listing_predicted_pe', '预测市盈率'],
429
+ ['listing_float_market_value', '流通值'],
430
+ ['listing_float_shares', '流通股'],
431
+ ['listing_pb_ratio', '市净率'],
432
+ ['listing_eps', 'EPS'],
433
+ ['listing_voting_rights_difference', '表决权差异'],
434
+ ['listing_registration_based', '是否注册制'],
435
+ ]);
436
+ }
437
+
438
+ export function mapTaxInvoiceFields(value) {
439
+ return scalarMap(value, [
440
+ ['tax_company_name', '企业名称'],
441
+ ['tax_identification_no', '纳税人识别号'],
442
+ ['tax_company_type', '企业类型'],
443
+ ['tax_business_status', '经营状态'],
444
+ ['invoice_address', '地址'],
445
+ ['invoice_phone', '联系电话'],
446
+ ['invoice_bank', '开户行'],
447
+ ['invoice_bank_account', '开户行账号'],
448
+ ]);
449
+ }
450
+
451
+ export function mapImportExportCreditFields(value) {
452
+ return scalarMap(value, [
453
+ ['import_export_credit_no', '统一社会信用代码'],
454
+ ['import_export_customs', '所在地海关'],
455
+ ['import_export_admin_division', '行政区划'],
456
+ ['import_export_address', '地址'],
457
+ ['import_export_economic_area', '经济区划'],
458
+ ['import_export_trade_type', '经营类别'],
459
+ ['import_export_statistical_economic_area', '统计经济区划'],
460
+ ['import_export_industry', '行业种类'],
461
+ ['import_export_ecommerce_type', '跨境贸易电子商务类型'],
462
+ ['import_export_credit_grade', '信用等级'],
463
+ ['import_export_filing_date', '备案日期'],
464
+ ]);
465
+ }
466
+
467
+ function countValue(value) {
468
+ if (value === '' || value === null || value === undefined) return '';
469
+ const number = Number(value);
470
+ return Number.isFinite(number) && number >= 0 ? number : '';
471
+ }
472
+
473
+ function selfRiskRows(value) {
474
+ return Array.isArray(value?.风险因子扫描) ? value.风险因子扫描.filter(isRecord) : [];
475
+ }
476
+
477
+ export function inspectSelfRiskCatalog(value) {
478
+ const applicable = Array.isArray(value?.风险因子扫描);
479
+ const actual = new Set(selfRiskRows(value).map((row) => String(row.风险因子 ?? '').trim()).filter(Boolean));
480
+ const expected = new Set(SELF_RISK_FACTORS.map(([, label]) => label));
481
+ return {
482
+ applicable,
483
+ version: RISK_FACTOR_CATALOG_VERSION,
484
+ missing: [...expected].filter((label) => !actual.has(label)),
485
+ unknown: [...actual].filter((label) => !expected.has(label)),
486
+ };
487
+ }
488
+
489
+ export function mapCompanyRiskScanFields(value) {
490
+ if (!isRecord(value) || !Array.isArray(value.风险因子扫描)) return {};
491
+ const counts = new Map(selfRiskRows(value).map((row) => [String(row.风险因子 ?? '').trim(), countValue(row.条目数)]));
492
+ const hits = SELF_RISK_FACTORS.flatMap(([, label]) => {
493
+ const count = counts.get(label);
494
+ return typeof count === 'number' && count > 0 ? [`${label}(${count})`] : [];
495
+ });
496
+ return {
497
+ risk_recorded_factor_count: countValue(value.有记录因子数),
498
+ risk_no_record_factor_count: countValue(value.无记录因子数),
499
+ risk_hit_summary: hits.join(';'),
500
+ ...Object.fromEntries(SELF_RISK_FACTORS.map(([id, label]) => [`risk_${id}_count`, counts.get(label) ?? ''])),
501
+ };
502
+ }
503
+
504
+ export function inspectRelatedRiskCatalog(value) {
505
+ const applicable = isRecord(value?.维度计数汇总);
506
+ // QCC MCP 当前契约名为“重要风险”;“关键风险”仅作早期预发环境兼容,
507
+ // 不得反向把兼容名称当成稳定上游契约。
508
+ const importantSource = value?.维度计数汇总?.重要风险 ?? value?.维度计数汇总?.关键风险;
509
+ const important = isRecord(importantSource) ? importantSource : {};
510
+ const locating = Array.isArray(value?.重点维度关联方定位) ? value.重点维度关联方定位.filter(isRecord) : [];
511
+ const importantActual = new Set(Object.keys(important));
512
+ const keyActual = new Set(locating.map((row) => String(row.维度 ?? '').trim()).filter(Boolean));
513
+ const importantExpected = new Set(RELATED_RISK_FACTORS.map(([, label]) => label));
514
+ const keyExpected = new Set(RELATED_RISK_KEY_FACTORS.map(([, label]) => label));
515
+ return {
516
+ applicable,
517
+ version: RISK_FACTOR_CATALOG_VERSION,
518
+ missing: [
519
+ ...[...importantExpected].filter((label) => !importantActual.has(label)).map((label) => `重要风险:${label}`),
520
+ ...[...keyExpected].filter((label) => !keyActual.has(label)).map((label) => `重点维度:${label}`),
521
+ ],
522
+ unknown: [
523
+ ...[...importantActual].filter((label) => !importantExpected.has(label)).map((label) => `重要风险:${label}`),
524
+ ...[...keyActual].filter((label) => !keyExpected.has(label)).map((label) => `重点维度:${label}`),
525
+ ],
526
+ };
527
+ }
528
+
529
+ export function mapCompanyRelatedRiskScanFields(value) {
530
+ if (!isRecord(value) || !isRecord(value.维度计数汇总)) return {};
531
+ const importantSource = value.维度计数汇总.重要风险 ?? value.维度计数汇总.关键风险;
532
+ const important = isRecord(importantSource) ? importantSource : {};
533
+ const locating = Array.isArray(value.重点维度关联方定位) ? value.重点维度关联方定位.filter(isRecord) : [];
534
+ const partyCounts = new Map(locating.map((row) => [String(row.维度 ?? '').trim(), countValue(row.命中关联方数)]));
535
+ const hits = RELATED_RISK_FACTORS.flatMap(([, label]) => {
536
+ const count = countValue(important[label]);
537
+ return typeof count === 'number' && count > 0 ? [`${label}(${count})`] : [];
538
+ });
539
+ const partyCount = countValue(value.有风险关联方数);
540
+ return {
541
+ related_risk_party_count: partyCount,
542
+ related_risk_summary: `${partyCount === '' ? '' : `有风险关联方${partyCount}个`}${hits.length ? `${partyCount === '' ? '' : ';'}${hits.join(';')}` : ''}`,
543
+ ...Object.fromEntries(RELATED_RISK_FACTORS.map(([id, label]) => [`related_risk_${id}_count`, countValue(important[label])])),
544
+ ...Object.fromEntries(RELATED_RISK_KEY_FACTORS.map(([id, label]) => [`related_risk_${id}_party_count`, partyCounts.get(label) ?? ''])),
383
545
  };
384
546
  }
385
547
 
386
548
  const LEGACY_ENRICHMENT_FIELDS = Object.freeze([
387
- 'credit_no', 'legal_rep', 'reg_capital', 'establish_date', 'reg_status', 'biz_status',
549
+ 'credit_no', 'legal_rep', 'reg_capital', 'establish_date', 'reg_status',
388
550
  ]);
389
- const PROFILE_FIELDS = new Set(['industry_large', 'industry_middle', 'company_profile']);
551
+ const SOURCE_TOOL_CONFIG = Object.freeze({
552
+ get_company_registration_info: Object.freeze({ name: QCC_TOOL_NAMES.registration, map: mapRegistrationFields }),
553
+ get_company_profile: Object.freeze({ name: QCC_TOOL_NAMES.profile, map: mapProfileFields }),
554
+ get_contact_info: Object.freeze({ name: QCC_TOOL_NAMES.contact, map: mapContactFields, args: { excludeInvalidPhone: false } }),
555
+ get_listing_info: Object.freeze({ name: QCC_TOOL_NAMES.listing, map: mapListingFields }),
556
+ get_tax_invoice_info: Object.freeze({ name: QCC_TOOL_NAMES.taxInvoice, map: mapTaxInvoiceFields }),
557
+ get_import_export_credit: Object.freeze({ name: QCC_TOOL_NAMES.importExportCredit, map: mapImportExportCreditFields }),
558
+ get_company_risk_scan: Object.freeze({ name: QCC_TOOL_NAMES.riskScan, map: mapCompanyRiskScanFields, inspect: inspectSelfRiskCatalog }),
559
+ get_company_related_risk_scan: Object.freeze({ name: QCC_TOOL_NAMES.relatedRiskScan, map: mapCompanyRelatedRiskScanFields, inspect: inspectRelatedRiskCatalog }),
560
+ });
561
+
562
+ export function sourceToolsForFieldSelection(fieldSelection, includeRisk = false) {
563
+ const selected = Array.isArray(fieldSelection) && fieldSelection.length ? fieldSelection : LEGACY_ENRICHMENT_FIELDS;
564
+ const tools = selectedSourceTools(selected, LEGACY_ENRICHMENT_FIELDS);
565
+ if (includeRisk && !tools.includes('get_company_risk_scan')) tools.push('get_company_risk_scan');
566
+ return tools;
567
+ }
390
568
 
391
- function requiresProfile(fieldSelection) {
392
- return Array.isArray(fieldSelection) && fieldSelection.some((field) => PROFILE_FIELDS.has(field));
569
+ export function estimateQccCalls(uniqueCompanies, fieldSelection, includeRisk = false) {
570
+ const companies = Math.max(0, Math.trunc(Number(uniqueCompanies) || 0));
571
+ const sourceTools = sourceToolsForFieldSelection(fieldSelection, includeRisk);
572
+ return {
573
+ uniqueCompanies: companies,
574
+ sourceTools,
575
+ callsPerCompany: 1 + sourceTools.length,
576
+ estimatedCalls: companies * (1 + sourceTools.length),
577
+ };
393
578
  }
394
579
 
395
580
  function mergeMappedFields(...sources) {
@@ -481,7 +666,12 @@ export class QccHostBridge {
481
666
  entityLookup: this.has(QCC_TOOL_NAMES.entityLookup),
482
667
  registration: this.has(QCC_TOOL_NAMES.registration),
483
668
  profile: this.has(QCC_TOOL_NAMES.profile),
669
+ contact: this.has(QCC_TOOL_NAMES.contact),
670
+ listing: this.has(QCC_TOOL_NAMES.listing),
671
+ taxInvoice: this.has(QCC_TOOL_NAMES.taxInvoice),
672
+ importExportCredit: this.has(QCC_TOOL_NAMES.importExportCredit),
484
673
  riskScan: this.has(QCC_TOOL_NAMES.riskScan),
674
+ relatedRiskScan: this.has(QCC_TOOL_NAMES.relatedRiskScan),
485
675
  };
486
676
  const ready = capabilities.entityLookup && capabilities.registration;
487
677
  return {
@@ -720,28 +910,39 @@ export class QccHostBridge {
720
910
  );
721
911
  const match = classifyEntityMatch(lookup.data);
722
912
  if (match.status !== 'exact') return match;
913
+ return this.enrichMatchedCompany(match, options);
914
+ }
723
915
 
724
- const lockedKey = match.creditNo || match.companyName;
725
- const registration = await this.call(
726
- QCC_TOOL_NAMES.registration,
727
- { searchKey: lockedKey },
728
- options,
729
- );
730
- let mapped = mapRegistrationFields(registration.data, match);
731
- if (requiresProfile(options.fieldSelection)) {
732
- const profile = await this.call(QCC_TOOL_NAMES.profile, { searchKey: lockedKey }, options);
733
- mapped = mergeMappedFields(mapped, mapProfileFields(profile.data));
916
+ async enrichMatchedCompany(match, options = {}) {
917
+ const lockedKey = String(match?.creditNo || match?.companyName || '').trim();
918
+ const sourceTools = sourceToolsForFieldSelection(options.fieldSelection, options.includeRisk);
919
+ let mapped = {};
920
+ let legacyRiskTags = '';
921
+ for (const sourceTool of sourceTools) {
922
+ const config = SOURCE_TOOL_CONFIG[sourceTool];
923
+ if (!config) continue;
924
+ const response = await this.call(config.name, { searchKey: lockedKey, ...(config.args ?? {}) }, options);
925
+ const fields = config.map(response.data, match);
926
+ mapped = mergeMappedFields(mapped, fields);
927
+ if (sourceTool === 'get_company_risk_scan') legacyRiskTags = mapRiskTags(response.data);
928
+ if (config.inspect) {
929
+ const catalog = config.inspect(response.data);
930
+ if (catalog.applicable && (catalog.missing.length || catalog.unknown.length)) {
931
+ emitAudit(options, {
932
+ toolName: response.toolName,
933
+ callId: response.callId,
934
+ outcome: 'catalog-drift',
935
+ code: 'QCC_RISK_CATALOG_DRIFT',
936
+ catalogVersion: catalog.version,
937
+ missing: catalog.missing,
938
+ unknown: catalog.unknown,
939
+ });
940
+ }
941
+ }
734
942
  }
735
943
  const fields = projectSelectedFields(mapped, options.fieldSelection);
736
- if (options.includeRisk) {
737
- const risk = await this.call(QCC_TOOL_NAMES.riskScan, { searchKey: lockedKey }, options);
738
- fields.risk_tags = mapRiskTags(risk.data);
739
- }
740
- return {
741
- status: 'enriched',
742
- companyName: match.companyName,
743
- fields,
744
- };
944
+ if (options.includeRisk) fields.risk_tags = legacyRiskTags;
945
+ return { status: 'enriched', companyName: String(match?.companyName ?? ''), fields };
745
946
  }
746
947
 
747
948
  async enrichLockedCompany(selection, options = {}) {
@@ -750,22 +951,7 @@ export class QccHostBridge {
750
951
  if (!creditNo) {
751
952
  throw new QccBridgeError('QCC_CANDIDATE_INVALID', 'A selected QCC candidate must include a credit number');
752
953
  }
753
- const registration = await this.call(
754
- QCC_TOOL_NAMES.registration,
755
- { searchKey: creditNo },
756
- options,
757
- );
758
- let mapped = mapRegistrationFields(registration.data, { companyName, creditNo });
759
- if (requiresProfile(options.fieldSelection)) {
760
- const profile = await this.call(QCC_TOOL_NAMES.profile, { searchKey: creditNo }, options);
761
- mapped = mergeMappedFields(mapped, mapProfileFields(profile.data));
762
- }
763
- const fields = projectSelectedFields(mapped, options.fieldSelection);
764
- if (options.includeRisk) {
765
- const risk = await this.call(QCC_TOOL_NAMES.riskScan, { searchKey: creditNo }, options);
766
- fields.risk_tags = mapRiskTags(risk.data);
767
- }
768
- return { status: 'enriched', companyName, fields };
954
+ return this.enrichMatchedCompany({ companyName, creditNo }, options);
769
955
  }
770
956
 
771
957
  async enrichRows(rows, options = {}) {
@@ -777,9 +963,27 @@ export class QccHostBridge {
777
963
  });
778
964
  }
779
965
 
780
- const requiredTools = [QCC_TOOL_NAMES.entityLookup, QCC_TOOL_NAMES.registration];
781
- if (requiresProfile(options.fieldSelection)) requiredTools.push(QCC_TOOL_NAMES.profile);
782
- if (options.includeRisk) requiredTools.push(QCC_TOOL_NAMES.riskScan);
966
+ const nameField = String(options.nameField ?? 'name');
967
+ const normalized = rows.map((row, index) => ({
968
+ index,
969
+ row: isRecord(row) ? { ...row } : {},
970
+ companyName: String(isRecord(row) ? row[nameField] ?? '' : '').trim(),
971
+ }));
972
+ const names = [...new Set(normalized.map((item) => item.companyName).filter(Boolean))];
973
+ const estimate = estimateQccCalls(names.length, options.fieldSelection, options.includeRisk);
974
+ const maxCalls = Math.max(1, Math.trunc(options.maxCalls ?? DEFAULT_MAX_CALLS));
975
+ if (estimate.estimatedCalls > maxCalls) {
976
+ throw new QccBridgeError('QCC_CALL_BUDGET_EXCEEDED', `QCC call estimate exceeds ${maxCalls}`, {
977
+ details: { ...estimate, maxCalls },
978
+ });
979
+ }
980
+
981
+ const requiredTools = [
982
+ QCC_TOOL_NAMES.entityLookup,
983
+ ...estimate.sourceTools
984
+ .map((sourceTool) => SOURCE_TOOL_CONFIG[sourceTool]?.name)
985
+ .filter(Boolean),
986
+ ];
783
987
  try {
784
988
  await Promise.all(requiredTools.map((name) => this.waitForTool(name, {
785
989
  signal: options.signal,
@@ -796,13 +1000,6 @@ export class QccHostBridge {
796
1000
  });
797
1001
  }
798
1002
 
799
- const nameField = String(options.nameField ?? 'name');
800
- const normalized = rows.map((row, index) => ({
801
- index,
802
- row: isRecord(row) ? { ...row } : {},
803
- companyName: String(isRecord(row) ? row[nameField] ?? '' : '').trim(),
804
- }));
805
- const names = [...new Set(normalized.map((item) => item.companyName).filter(Boolean))];
806
1003
  const concurrency = Math.min(MAX_CONCURRENCY, Math.max(1, Math.trunc(options.concurrency ?? 2)));
807
1004
  let completedUnique = 0;
808
1005
 
@@ -857,6 +1054,6 @@ export class QccHostBridge {
857
1054
  includeRisk: Boolean(options.includeRisk),
858
1055
  };
859
1056
 
860
- return { summary, rows: outputRows, reviewQueue, errors };
1057
+ return { summary, rows: outputRows, reviewQueue, errors, estimate };
861
1058
  }
862
1059
  }
package/lib/web.js CHANGED
@@ -16,6 +16,13 @@ import { PHASE3_BATCH_LIMITS, Phase3BatchService, Phase3RunStore } from './qcc-p
16
16
  import { publicWorkflowContract } from './workflow-contract.js';
17
17
  import { DataCleaningWorkflowStore, WorkflowError } from './workflow.js';
18
18
  import { ArtifactError, WorkflowArtifactStore } from './artifacts.js';
19
+ import {
20
+ ImageIntakeError,
21
+ ImageIntakeStore,
22
+ registerImageIntakeTool,
23
+ serializeImageExtractionPrompt,
24
+ TOOL_IMAGE_EXTRACT,
25
+ } from './image-intake.js';
19
26
 
20
27
  const MAX_BODY = 16 * 1024 * 1024; // 16 MiB 上传上限(MVP)
21
28
 
@@ -119,6 +126,17 @@ function writeWorkflowError(res, error) {
119
126
  });
120
127
  }
121
128
 
129
+ function writeImageError(res, error) {
130
+ if (error instanceof ImageIntakeError) {
131
+ return writeJson(res, error.status, { ok: false, code: error.code, message: error.message });
132
+ }
133
+ return writeJson(res, 500, {
134
+ ok: false,
135
+ code: 'DC_IMAGE_INTERNAL',
136
+ message: '图片名单接入请求失败。',
137
+ });
138
+ }
139
+
122
140
  /** 从 JSON 协议解析上传:{ filename, content }。content 为字符串;xlsx 时为 base64。 */
123
141
  async function parseUpload(body) {
124
142
  let payload;
@@ -285,13 +303,17 @@ export function mountWebRoutes(wctx, { logger, report, TOOL_NAME, SKILL_NAME })
285
303
  const skills = wctx.skills;
286
304
  const disposers = [];
287
305
  const qccBridge = new QccHostBridge({ tools, logger });
306
+ const imageIntake = new ImageIntakeStore({ tools });
288
307
  const g5Runs = new G5RunStore();
289
308
  const qccCommands = new QccCommandStore({ bridge: qccBridge, runs: g5Runs });
290
309
  if (typeof tools?.register === 'function') {
291
310
  disposers.push(registerQccCommandTool(tools, qccCommands));
311
+ disposers.push(registerImageIntakeTool(tools, imageIntake));
292
312
  report.qccCommandToolRegistered = true;
313
+ report.imageIntakeToolRegistered = true;
293
314
  } else {
294
315
  report.qccCommandToolRegistered = false;
316
+ report.imageIntakeToolRegistered = false;
295
317
  }
296
318
  const phase3Service = new Phase3BatchService(qccBridge);
297
319
  const phase3Runs = new Phase3RunStore();
@@ -347,11 +369,64 @@ export function mountWebRoutes(wctx, { logger, report, TOOL_NAME, SKILL_NAME })
347
369
  workflowV2: Boolean(wctx.storageDomain),
348
370
  durableArtifacts: Boolean(artifactStore),
349
371
  artifactBinaryStrategy: artifactStore ? 'xlsx-base64-over-writeText' : 'unavailable',
372
+ imageIntake: imageIntake.capabilities(),
350
373
  qccBridge: qccBridge.capabilities(),
351
374
  },
352
375
  });
353
376
  });
354
377
 
378
+ register('/data-cleaning/api/images/capabilities', (req, res) => {
379
+ if (!isTrusted(req)) return writeJson(res, 403, { ok: false, error: 'untrusted origin' });
380
+ if (req.method !== 'GET') return writeJson(res, 405, { ok: false, code: 'DC_METHOD', message: 'GET required' });
381
+ writeJson(res, 200, {
382
+ ok: true,
383
+ marker: 'data-cleaning-image-intake-v1',
384
+ tool: TOOL_IMAGE_EXTRACT,
385
+ toolRegistered: report.imageIntakeToolRegistered === true,
386
+ capabilities: imageIntake.capabilities(),
387
+ qccCalls: false,
388
+ });
389
+ });
390
+
391
+ register('/data-cleaning/api/images/commands', async (req, res) => {
392
+ if (!isTrusted(req)) return writeJson(res, 403, { ok: false, error: 'untrusted origin' });
393
+ try {
394
+ const pathname = new URL(req.url ?? '/data-cleaning/api/images/commands', 'http://127.0.0.1').pathname;
395
+ const segments = pathname.split('/').filter(Boolean);
396
+ const commandsIndex = segments.indexOf('commands');
397
+ const rest = commandsIndex >= 0 ? segments.slice(commandsIndex + 1) : [];
398
+ if (rest.length === 0 && req.method === 'POST') {
399
+ if (report.imageIntakeToolRegistered !== true) {
400
+ throw new ImageIntakeError('DC_IMAGE_TOOL_UNAVAILABLE', '当前 DSH Host 无法注册图片名单高层工具。', 503);
401
+ }
402
+ const payload = JSON.parse((await readBody(req)).toString('utf8'));
403
+ const command = await imageIntake.prepare(payload);
404
+ return writeJson(res, 201, {
405
+ ok: true,
406
+ marker: 'data-cleaning-image-intake-v1',
407
+ command: { ...command, prompt: serializeImageExtractionPrompt(command) },
408
+ });
409
+ }
410
+ if (rest.length === 1 && req.method === 'GET') {
411
+ return writeJson(res, 200, {
412
+ ok: true,
413
+ marker: 'data-cleaning-image-intake-v1',
414
+ command: imageIntake.status(decodeURIComponent(rest[0])),
415
+ });
416
+ }
417
+ if (rest.length === 1 && req.method === 'DELETE') {
418
+ await imageIntake.remove(decodeURIComponent(rest[0]));
419
+ return writeJson(res, 200, { ok: true, marker: 'data-cleaning-image-intake-v1', removed: true });
420
+ }
421
+ return writeJson(res, 405, { ok: false, code: 'DC_METHOD', message: 'POST a command, GET its status, or DELETE it.' });
422
+ } catch (error) {
423
+ if (error instanceof SyntaxError) {
424
+ return writeJson(res, 400, { ok: false, code: 'DC_BAD_JSON', message: 'Request body must be valid JSON.' });
425
+ }
426
+ return writeImageError(res, error);
427
+ }
428
+ });
429
+
355
430
  register('/data-cleaning/api/workflow/contract', (req, res) => {
356
431
  if (!isTrusted(req)) return writeJson(res, 403, { ok: false, error: 'untrusted origin' });
357
432
  if (req.method !== 'GET') {
@@ -955,6 +1030,7 @@ export function mountWebRoutes(wctx, { logger, report, TOOL_NAME, SKILL_NAME })
955
1030
  return () => {
956
1031
  if (state) { state.dispose().catch(() => {}); }
957
1032
  if (workflow) { workflow.dispose().catch(() => {}); }
1033
+ imageIntake.dispose().catch(() => {});
958
1034
  for (const dispose of disposers) dispose();
959
1035
  };
960
1036
  }