case-intake.mjs 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. #!/usr/bin/env node
  2. // Copyright (c) 未来飞马
  3. //
  4. // This Source Code Form is subject to the terms of the Mozilla Public
  5. // License, v. 2.0. If a copy of the MPL was not distributed with this
  6. // file, You can obtain one at https://mozilla.org/MPL/2.0/.
  7. //
  8. // Trademark Notice:
  9. // The MPL-2.0 license grants copyright permissions for source code only.
  10. // It does NOT grant any rights to use trademarks including "未来飞马",
  11. // "Harness Loop", "RSI", and associated slogan "让AI进化提前发生,让AI落地快人一步".
  12. // Any use of these trademarks requires separate written permission.
  13. /**
  14. * skill-case-get —— 案例采集 / 拆解 / 归档 主入口
  15. *
  16. * 管线:
  17. * ① 输入识别:连续多图 / .docx / .pptx / 图文混合 / 裸图 / 视频
  18. * ② 拆解提取:图片→skill-vision(OCR+语义);docx/pptx→python3 标准库解包;视频→抽帧+skill-listen 转写
  19. * ③ 分组与顺序:尺寸/时间/内容相似度判同组;九宫格按左上→右下给显式 order
  20. * ④ 案例信息 vs 素材 分离
  21. * ⑤ 合规判断:能否入库?客诉/负面?→ riskFlags / privacyFindings
  22. * ⑥ 打标:静态属性 + 跟进状态七段 + 亮点/场景/异议 + 学校别名归一
  23. * ⑦ 入库:caseSubmit 写待审清单(reviewStatus=pending, readyForUse=false),带幂等键
  24. * ⑧ 字典自进化:新标签追加进 references/tag-dictionary.json
  25. *
  26. * 子命令:
  27. * plan 只识别 + 提取 + 分组(不调任何模型,纯本机)
  28. * ingest plan + 图片理解 + 分类打标 → 产出 case-package.json(默认不提交)
  29. * submit 读取 case-package.json(可先 reorder)→ 调云函数 caseSubmit
  30. * reorder 调整 materialAssets 顺序(支持数组 + 顺序调整)
  31. * dictionary 查看 / 追加标签字典
  32. * selftest 本机自检(office 解包 + 分组 + 打标 + 合规)
  33. *
  34. * 安全:绝不打印 token/PII;生成的产物默认落在 <cwd>/case-get-outputs/。
  35. */
  36. import fs from 'node:fs';
  37. import path from 'node:path';
  38. import zlib from 'node:zlib';
  39. import { spawnSync } from 'node:child_process';
  40. import { parseArgs } from 'node:util';
  41. import {
  42. TENANT_DEFAULT, SKILL_ROOT, buildIdempotencyKey, deriveAssetUrl, ensureDir, firstText, imageUrlsFrom,
  43. learnAliases, learnTags, loadDictionary, newRunId, normalizeMaterialAsset, outputsDir,
  44. readJson, reorderAssets, resolvePython, saveDictionary, sortAssets, truncate, uniq, writeJson,
  45. } from './lib.mjs';
  46. import { readImageInfo, IMAGE_EXTENSIONS, VIDEO_EXTENSIONS, DOC_EXTENSIONS, DECK_EXTENSIONS, TEXT_EXTENSIONS } from './image-size.mjs';
  47. import { applyGroupOverrides, groupMaterials, DEFAULT_ORDERING_RULE } from './grouping.mjs';
  48. import { classifyCase } from './classify.mjs';
  49. import { processVideo } from './listen-bridge.mjs';
  50. import { analyzeImages } from './vision-bridge.mjs';
  51. import { uploadMaterial, StorageUnavailableError } from './storage-bridge.mjs';
  52. import { CloudError, submitCase } from './cloud-client.mjs';
  53. import { submitViaParse, probeParse } from './parse-direct.mjs';
  54. // ---------------------------------------------------------------------------
  55. // 输入识别
  56. // ---------------------------------------------------------------------------
  57. export function detectSourceType({ images = 0, documents = 0, decks = 0, videos = 0, texts = 0 }) {
  58. const kinds = [images > 0, documents > 0, decks > 0, videos > 0, texts > 0].filter(Boolean).length;
  59. if (kinds > 1) return 'mixed';
  60. if (images > 1) return 'image_group';
  61. if (images === 1 && videos === 0) return 'single_image';
  62. if (videos > 0) return 'image_group'; // 视频素材组,按 group 处理
  63. if (decks > 0) return 'presentation';
  64. if (documents > 0) return 'document';
  65. return 'mixed';
  66. }
  67. export function classifyInput(filePath) {
  68. const ext = path.extname(filePath).toLowerCase();
  69. if (IMAGE_EXTENSIONS.has(ext)) return 'image';
  70. if (VIDEO_EXTENSIONS.has(ext)) return 'video';
  71. if (DOC_EXTENSIONS.has(ext)) return 'docx';
  72. if (DECK_EXTENSIONS.has(ext)) return 'pptx';
  73. if (TEXT_EXTENSIONS.has(ext)) return 'text';
  74. return 'unknown';
  75. }
  76. /** 展开目录(只展开一层里的文件,不递归太深,避免误扫)。 */
  77. export function expandInputs(inputs, maxDepth = 2) {
  78. const out = [];
  79. const walk = (target, depth) => {
  80. let stat;
  81. try {
  82. stat = fs.statSync(target);
  83. } catch {
  84. out.push({ path: target, exists: false });
  85. return;
  86. }
  87. if (stat.isDirectory()) {
  88. if (depth > maxDepth) return;
  89. for (const entry of fs.readdirSync(target).sort()) {
  90. if (entry.startsWith('.')) continue;
  91. walk(path.join(target, entry), depth + 1);
  92. }
  93. return;
  94. }
  95. out.push({ path: target, exists: true, size: stat.size, mtimeMs: stat.mtimeMs });
  96. };
  97. for (const input of inputs) walk(input, 0);
  98. return out;
  99. }
  100. // ---------------------------------------------------------------------------
  101. // ② 拆解提取
  102. // ---------------------------------------------------------------------------
  103. function runOfficeExtract(files, mediaOut) {
  104. if (!files.length) return { available: true, results: [], error: null };
  105. const script = path.join(SKILL_ROOT, 'scripts', 'office_extract.py');
  106. if (!fs.existsSync(script)) return { available: false, results: [], error: `缺少 ${script}` };
  107. const python = resolvePython();
  108. const args = [script, '--media-out', mediaOut];
  109. for (const file of files) args.push('--input', file);
  110. const run = spawnSync(python, args, { encoding: 'utf8', maxBuffer: 64 * 1024 * 1024 });
  111. if (run.error) {
  112. return { available: false, results: [], error: `${python} 启动失败:${run.error.message}(.docx/.pptx 解包需要 python3 标准库)` };
  113. }
  114. let parsed = null;
  115. try {
  116. parsed = JSON.parse(run.stdout || '{}');
  117. } catch {
  118. return { available: true, results: [], error: `office_extract 输出不是 JSON:${truncate(run.stderr || run.stdout, 300)}` };
  119. }
  120. return {
  121. available: true,
  122. results: parsed.results || [],
  123. error: parsed.ok ? null : `部分文档解包失败:${(parsed.results || []).filter((r) => !r.ok).map((r) => r.error).join(', ')}`,
  124. };
  125. }
  126. function sleep(ms) {
  127. return new Promise((resolve) => { setTimeout(resolve, ms); });
  128. }
  129. // ---------------------------------------------------------------------------
  130. // plan:识别 + 提取 + 分组
  131. // ---------------------------------------------------------------------------
  132. export async function runPlan(options = {}) {
  133. const outDir = ensureDir(options.outDir);
  134. const mediaDir = ensureDir(path.join(outDir, 'media'));
  135. const entries = expandInputs(options.inputs || []);
  136. const missing = entries.filter((e) => !e.exists).map((e) => e.path);
  137. const buckets = { image: [], video: [], docx: [], pptx: [], text: [], unknown: [] };
  138. for (const entry of entries) {
  139. if (!entry.exists) continue;
  140. buckets[classifyInput(entry.path)].push(entry.path);
  141. }
  142. const warnings = [];
  143. for (const file of missing) warnings.push(`输入不存在:${file}`);
  144. if (buckets.unknown.length) warnings.push(`忽略无法识别的文件:${buckets.unknown.join(', ')}`);
  145. // ---- docx / pptx 解包
  146. const docxResults = runOfficeExtract(buckets.docx, path.join(mediaDir, 'docx'));
  147. const pptxResults = runOfficeExtract(buckets.pptx, path.join(mediaDir, 'pptx'));
  148. if (docxResults.error) warnings.push(docxResults.error);
  149. if (pptxResults.error) warnings.push(pptxResults.error);
  150. const textBundle = { paragraphs: [], notes: [], titles: [], transcript: '' };
  151. const mediaFromDocs = [];
  152. for (const result of [...docxResults.results, ...pptxResults.results]) {
  153. if (!result.ok) {
  154. warnings.push(`${result.fileName || result.input} 解包失败:${result.error}`);
  155. continue;
  156. }
  157. for (const p of result.paragraphs || []) if (p.text) textBundle.paragraphs.push(p.text);
  158. for (const h of result.headings || []) if (h) textBundle.titles.push(h);
  159. for (const note of result.notes || []) {
  160. if (typeof note === 'string') textBundle.notes.push(note);
  161. else if (note && note.text) textBundle.notes.push(note.text);
  162. }
  163. for (const slide of result.slides || []) if (slide.title) textBundle.titles.push(slide.title);
  164. for (const media of result.media || []) {
  165. if (media.kind === 'image' && media.localPath) mediaFromDocs.push({ ...media, sourceDoc: result.fileName || result.input });
  166. }
  167. }
  168. // ---- 纯文本输入(.md/.txt):并入说明语料
  169. for (const file of buckets.text) {
  170. try {
  171. const text = fs.readFileSync(file, 'utf8');
  172. textBundle.paragraphs.push(...text.split(/\r?\n/).map((l) => l.trim()).filter(Boolean));
  173. } catch (error) {
  174. warnings.push(`文本读取失败 ${file}:${error.message}`);
  175. }
  176. }
  177. // ---- 图片:读尺寸/时间
  178. const imageItems = [];
  179. for (const file of buckets.image) {
  180. const info = readImageInfo(file);
  181. if (!info.ok) warnings.push(`图片信息读取失败 ${path.basename(file)}:${info.error}`);
  182. imageItems.push({
  183. kind: 'image',
  184. localPath: path.resolve(file),
  185. width: info.width || null,
  186. height: info.height || null,
  187. mtimeMs: info.mtimeMs || fs.statSync(file).mtimeMs,
  188. ocrText: '',
  189. description: '',
  190. label: path.basename(file),
  191. origin: 'direct',
  192. });
  193. }
  194. // 文档内嵌图片也参与分组(图文混合)。sourceKey 钉住来源文档,
  195. // 避免两份不同文档的配图因为同比例、同落盘时间被误并成一组。
  196. for (const media of mediaFromDocs) {
  197. const info = readImageInfo(media.localPath);
  198. imageItems.push({
  199. kind: 'image',
  200. localPath: media.localPath,
  201. width: info.width || null,
  202. height: info.height || null,
  203. mtimeMs: info.mtimeMs || null,
  204. ocrText: '',
  205. description: media.nearbyText || '',
  206. label: `${media.sourceDoc || '文档'} 内嵌图`,
  207. origin: 'document-media',
  208. sourceKey: media.sourceDoc || '',
  209. slide: media.slide,
  210. });
  211. }
  212. // ---- 视频:抽帧 + 转写
  213. const videoReports = [];
  214. for (const file of buckets.video) {
  215. if (options.skipVideo) {
  216. warnings.push(`跳过视频(--skip-video):${path.basename(file)}`);
  217. continue;
  218. }
  219. const report = await processVideo(file, {
  220. workDir: path.join(mediaDir, 'video', path.basename(file, path.extname(file))),
  221. frameCount: options.frameCount || 6,
  222. diarize: Boolean(options.diarize),
  223. });
  224. videoReports.push(report);
  225. for (const warning of report.warnings) warnings.push(`${path.basename(file)}:${warning}`);
  226. if (report.transcription && report.transcription.text) {
  227. textBundle.transcript = [textBundle.transcript, report.transcription.text].filter(Boolean).join('\n');
  228. }
  229. for (const frame of report.frames || []) {
  230. imageItems.push({
  231. kind: 'image',
  232. localPath: frame.localPath,
  233. width: null,
  234. height: null,
  235. mtimeMs: null,
  236. ocrText: '',
  237. description: `视频抽帧 @${frame.atMs != null ? `${Math.round(frame.atMs / 1000)}s` : '开头'}`,
  238. label: `${path.basename(file)} 抽帧`,
  239. origin: 'video-frame',
  240. atMs: frame.atMs,
  241. videoPath: path.resolve(file),
  242. });
  243. }
  244. }
  245. // ---- 分组(尺寸 / 时间 / 内容)
  246. let groups = groupMaterials(imageItems, options.grouping || {});
  247. groups = applyGroupOverrides(groups, options.groupOverrides);
  248. const sourceType = options.sourceType || detectSourceType({
  249. images: buckets.image.length,
  250. documents: buckets.docx.length + buckets.text.length,
  251. decks: buckets.pptx.length,
  252. videos: buckets.video.length,
  253. });
  254. const plan = {
  255. runId: options.runId || newRunId(),
  256. generatedAt: new Date().toISOString(),
  257. tenantId: options.tenantId || TENANT_DEFAULT,
  258. sourceType,
  259. sourceRef: options.sourceRef || '',
  260. authorizationStatus: String(options.authorizationStatus || '').toLowerCase(),
  261. inputs: {
  262. images: buckets.image.map((p) => path.resolve(p)),
  263. documents: buckets.docx.map((p) => path.resolve(p)),
  264. decks: buckets.pptx.map((p) => path.resolve(p)),
  265. videos: buckets.video.map((p) => path.resolve(p)),
  266. texts: buckets.text.map((p) => path.resolve(p)),
  267. },
  268. counts: {
  269. imageCount: imageItems.length,
  270. documentCount: buckets.docx.length,
  271. deckCount: buckets.pptx.length,
  272. videoCount: buckets.video.length,
  273. groupCount: groups.length,
  274. },
  275. textBundle,
  276. groups,
  277. videoReports,
  278. warnings,
  279. // 生成物绝对路径(后续 vision / submit 直接引用)
  280. artifacts: {
  281. extracted: path.join(outDir, 'extracted.json'),
  282. groupPlan: path.join(outDir, 'group-plan.json'),
  283. },
  284. };
  285. if (!groups.length) plan.warnings.push('没有识别到任何可用素材(没有任何图片/视频/文档内嵌图)');
  286. writeJson(plan.artifacts.extracted, { textBundle, mediaFromDocs, videoReports });
  287. writeJson(plan.artifacts.groupPlan, { sourceType, groups });
  288. return plan;
  289. }
  290. // ---------------------------------------------------------------------------
  291. // ingest:plan + 图片理解 + 分类打标 → 案例包
  292. // ---------------------------------------------------------------------------
  293. /**
  294. * 解析单个素材的公网 URL(materialAssets[].url 的唯一来源)。
  295. *
  296. * 优先级(任务书硬要求):
  297. * ① item.url 已存在(文档自带的远程 URL)→ 原样用,**不触发上传**;
  298. * ② 本地文件且 upload 开 → 真上传(fmode-storage 第 0 级免密钥通道)拿公网 URL;
  299. * ③ --asset-base-url 派生(保留旧行为,显式回退);
  300. * ④ 都没有 → 空字符串(由调用方补 warning)。
  301. *
  302. * 本函数**不抛错、不打印**:上传失败以 `error` 返回,由调用方降级并汇总告警,
  303. * 保证「单张失败不拖垮整个 ingest」。
  304. *
  305. * @returns {Promise<{url:string, upload:object|null, fallback:boolean, error:Error|null, reason:string}>}
  306. */
  307. export async function resolveAssetUrl(item, options = {}) {
  308. const remote = item && item.url ? String(item.url) : '';
  309. if (remote) return { url: remote, upload: null, fallback: false, error: null, reason: 'remote-url' };
  310. const localPath = (item && item.localPath) || '';
  311. let uploadError = null;
  312. if (localPath && options.upload) {
  313. try {
  314. const upload = await uploadMaterial(localPath, {
  315. tenantId: options.tenantId,
  316. // slug 区分度由 storage-bridge 统一兜底(slugify 为空时用内容指纹),这里原样传
  317. caseSlug: options.caseSlug,
  318. order: options.order,
  319. });
  320. if (upload && upload.url) {
  321. return { url: upload.url, upload, fallback: false, error: null, reason: 'uploaded' };
  322. }
  323. uploadError = new Error('上传回执未给出公网 URL');
  324. } catch (error) {
  325. // 上传失败:**记下原因**并继续走回退链,绝不吞掉(调用方要据此告警)
  326. uploadError = error;
  327. }
  328. }
  329. const derived = deriveAssetUrl(localPath, options.assetBaseUrl);
  330. if (derived) return { url: derived, upload: null, fallback: true, error: uploadError, reason: 'derived' };
  331. return {
  332. url: '',
  333. upload: null,
  334. fallback: false,
  335. error: uploadError,
  336. reason: localPath ? 'no-url' : 'no-source',
  337. };
  338. }
  339. export async function runIngest(options = {}) {
  340. const plan = await runPlan(options);
  341. const outDir = options.outDir;
  342. const warnings = [...plan.warnings];
  343. // 图片理解(OCR + 语义)——没有图片时跳过
  344. const allImages = plan.groups.flatMap((g) => g.items.filter((i) => i.kind === 'image'));
  345. let vision = { available: true, results: [], error: null, needsHostRead: false };
  346. if (options.skipVision) {
  347. warnings.push('跳过图片理解(--skip-vision):素材只有文件名,ocrText/description 为空');
  348. } else if (allImages.length) {
  349. const batchHint = [
  350. plan.textBundle.titles.slice(0, 3).join(' / '),
  351. plan.textBundle.paragraphs.slice(0, 3).join(' / '),
  352. ].filter(Boolean).join(' | ');
  353. vision = await analyzeImages(
  354. allImages.map((item) => ({ localPath: item.localPath, url: item.url, label: item.label })),
  355. { model: options.visionModel, batchHint },
  356. );
  357. if (!vision.available) {
  358. warnings.push(vision.error);
  359. } else if (vision.needsHostRead) {
  360. warnings.push('宿主多模态模式:vision 返回了读图指令(instruction),请宿主 Agent 用 Read 工具读图后把 JSON 结果写入 vision-host.json,再执行 submit。');
  361. } else if (vision.error) {
  362. warnings.push(vision.error);
  363. }
  364. }
  365. // 把 vision 结果并回素材
  366. const visionByFile = new Map();
  367. for (const result of vision.results || []) {
  368. if (result.file) visionByFile.set(path.resolve(result.file), result);
  369. }
  370. const enrichedGroups = plan.groups.map((group) => ({
  371. ...group,
  372. items: group.items.map((item) => {
  373. const enriched = { ...item };
  374. if (item.kind === 'image' && item.localPath) {
  375. const found = visionByFile.get(path.resolve(item.localPath));
  376. if (found) {
  377. enriched.ocrText = found.ocrText || enriched.ocrText || '';
  378. enriched.description = found.description || enriched.description || '';
  379. enriched.usageSuggestion = found.usageSuggestion || '';
  380. enriched.label = found.label || enriched.label || '';
  381. // 分析未产出结论(宿主读图待补 / 分析失败)时保持默认 role='material',
  382. // 不能凭空把素材降级成说明配图。
  383. enriched.role = found.role || 'material';
  384. enriched.piiHints = found.piiHints || [];
  385. enriched.visionPending = Boolean(found.needsHostRead);
  386. } else {
  387. enriched.role = enriched.role || 'material';
  388. }
  389. }
  390. return enriched;
  391. }),
  392. }));
  393. // 案例信息 vs 素材:分离后分类打标
  394. const dict = loadDictionary(options.dictPath);
  395. const visionResults = (vision.results || []).map((r) => ({ ...r }));
  396. const classification = classifyCase({
  397. dict,
  398. groups: enrichedGroups,
  399. visionResults,
  400. textBundle: plan.textBundle,
  401. authorizationStatus: plan.authorizationStatus,
  402. hints: options.hints || {},
  403. });
  404. // ⑧ 字典自进化
  405. const learn = learnTags(dict, classification.learned.tags);
  406. const aliasLearn = learnAliases(dict, classification.learned.aliases);
  407. const dictionaryChanges = { tags: learn.added, aliases: aliasLearn.added, skipped: learn.skipped };
  408. const dictionaryFile = options.dictPath ? path.resolve(options.dictPath) : undefined;
  409. if ((learn.added.length || aliasLearn.added.length) && !options.noLearn) {
  410. saveDictionary(dict);
  411. } else if (options.noLearn && (learn.added.length || aliasLearn.added.length)) {
  412. warnings.push('字典自进化被 --no-learn 关闭:本次新增标签未落盘');
  413. dictionaryChanges.tags = [];
  414. dictionaryChanges.aliases = [];
  415. }
  416. if (dictionaryFile) writeJson(dictionaryFile, readJson(dictionaryFile, {}));
  417. // ---- 组装 materialAssets[](逐字对齐 SHARED-CONTRACT.md)
  418. const materialAssets = [];
  419. const groupInfoList = [];
  420. const assetProblems = [];
  421. let missingUrlCount = 0;
  422. let uploadFailedCount = 0;
  423. let storageUnavailable = false;
  424. const fallbackItems = [];
  425. // 上传真正发生才需要 caseSlug;全走本地文件时没有标题也不影响。
  426. const caseSlugSeed = firstText(options.sourceRef, options.title);
  427. for (const group of enrichedGroups) {
  428. groupInfoList.push({
  429. groupId: group.groupId,
  430. layout: group.layout,
  431. count: group.count,
  432. orderingRule: group.orderingRule,
  433. });
  434. for (const item of sortAssets(group.items)) {
  435. // URL 优先级:① 文档自带远程 url → ② --upload 真上传 → ③ --asset-base-url 派生 → ④ 留空 + warning
  436. const resolved = await resolveAssetUrl(item, {
  437. // 默认开(与 CLI 一致):只有显式 upload:false / --no-upload 才关
  438. upload: options.upload !== false,
  439. tenantId: plan.tenantId,
  440. // 这里**不要**用 'case' 兜底:留空让 resolveAssetUrl 用文件内容指纹保证 key 区分度
  441. caseSlug: item.slug || caseSlugSeed || '',
  442. order: materialAssets.length + 1,
  443. assetBaseUrl: options.assetBaseUrl,
  444. });
  445. const assetUrl = resolved.url;
  446. const upload = resolved.upload;
  447. const uploadError = resolved.error;
  448. const usedFallback = resolved.fallback;
  449. if (uploadError) {
  450. // 上传失败绝不能拖垮整个 ingest:单张降级、明确告警,其余素材照常
  451. uploadFailedCount += 1;
  452. if (uploadError instanceof StorageUnavailableError) storageUnavailable = true;
  453. const reason = uploadError && uploadError.message ? uploadError.message : String(uploadError);
  454. warnings.push(`上传失败(${path.basename(item.localPath)}):${reason};该素材将无公网 URL`);
  455. }
  456. if (!assetUrl && item.localPath) {
  457. missingUrlCount += 1;
  458. if (!uploadError) fallbackItems.push(path.basename(item.localPath));
  459. }
  460. const { asset, problems } = normalizeMaterialAsset({
  461. order: item.order,
  462. groupId: group.groupId,
  463. kind: item.kind,
  464. url: assetUrl,
  465. localPath: item.localPath || '',
  466. label: item.label || '',
  467. ocrText: item.ocrText || '',
  468. description: item.description || '',
  469. usageSuggestion: item.usageSuggestion || '',
  470. role: item.role || 'material',
  471. }, materialAssets.length);
  472. assetProblems.push(...problems);
  473. materialAssets.push({
  474. ...asset,
  475. ...(upload ? { uploadKey: upload.key, uploadBytes: upload.bytes, uploadVia: 'cloud-function' } : {}),
  476. isFallbackUrl: usedFallback || undefined,
  477. });
  478. }
  479. }
  480. if (uploadFailedCount) {
  481. warnings.push(`${uploadFailedCount} 张上传失败:这些素材的 url 已降级(本地派生前缀/留空),检索侧拿不到它们的图。`
  482. + (storageUnavailable ? '(fmode-storage 不可用:安装兄弟技能或改用 --no-upload + --asset-base-url)' : ''));
  483. }
  484. for (const problem of assetProblems) warnings.push(problem);
  485. if (missingUrlCount) {
  486. const detail = options.upload === false
  487. ? '(--no-upload 已关闭上传;可改用 --asset-base-url <CDN前缀> 派生)'
  488. : `已尝试上传但未拿到公网地址(${uniq(fallbackItems).slice(0, 5).join('、') || '见上方上传失败原因'})。请确认 fmode-storage 可用(免密钥通道)或改用 --asset-base-url <CDN前缀> 派生(检索侧只看 url)`;
  489. warnings.push(`${missingUrlCount} 个素材只有本地路径、没有 URL:${detail}。`);
  490. }
  491. // 全局重编号:materialAssets 已按「分组顺序 → 组内 order」构建,
  492. // 这里只顺次改写 order。**不能**再按 order 全局排序——各组的 order 都从 1 起,
  493. // 直接排会把四组的第一张图并到最前面。
  494. const orderedAssets = materialAssets.map((asset, index) => ({ ...asset, order: index + 1 }));
  495. const imageUrls = imageUrlsFrom(orderedAssets);
  496. // ---- 组装案例包(幂等键:tenantId + 内容指纹)
  497. const title = firstText(options.title, classification.caseFields.title);
  498. const idempotencyKey = buildIdempotencyKey({
  499. tenantId: plan.tenantId,
  500. sourceType: plan.sourceType,
  501. sourceRef: plan.sourceRef,
  502. title,
  503. materialAssets: orderedAssets,
  504. explicit: options.idempotencyKey,
  505. });
  506. const primaryGroup = groupInfoList[0] || { groupId: 'g1', layout: '1x1', count: 0, orderingRule: DEFAULT_ORDERING_RULE };
  507. const casePackage = {
  508. schemaVersion: '1.0.0',
  509. generatedAt: new Date().toISOString(),
  510. runId: plan.runId,
  511. contract: 'SHARED-CONTRACT.md',
  512. // ---- 云函数 caseSubmit 入参(逐字对齐)
  513. sourceType: plan.sourceType,
  514. sourceRef: plan.sourceRef,
  515. idempotencyKey,
  516. title,
  517. summary: firstText(options.summary, classification.caseFields.summary),
  518. materialAssets: orderedAssets,
  519. groupInfo: primaryGroup,
  520. groupInfoList,
  521. schoolCanonical: (classification.tags.schoolCanonical || [])[0] || '',
  522. tags: uniq([
  523. ...(classification.tags.country || []),
  524. ...(classification.tags.schoolCanonical || []),
  525. ...(classification.tags.major || []),
  526. ...(classification.tags.stage || []),
  527. ...(classification.tags.subject || []),
  528. ...(classification.tags.highlightTypes || []),
  529. ...(classification.tags.scenarioTags || []),
  530. ...(classification.tags.objectionTags || []),
  531. ...(classification.fit.fitStatus || []),
  532. ...(classification.tags.productLine || []),
  533. ]),
  534. highlightTypes: classification.tags.highlightTypes || [],
  535. scenarioTags: classification.tags.scenarioTags || [],
  536. objectionTags: classification.tags.objectionTags || [],
  537. riskFlags: classification.compliance.riskFlags || [],
  538. authorizationStatus: plan.authorizationStatus || '',
  539. // ---- 案例字段(案例说明,与素材分开)
  540. targetCustomer: firstText(options.targetCustomer, classification.caseFields.targetCustomer),
  541. usageSuggestion: firstText(options.usageSuggestion, classification.caseFields.usageSuggestion),
  542. resultEvidence: classification.caseFields.resultEvidence,
  543. outcome: classification.caseFields.outcome,
  544. country: (classification.tags.country || [])[0] || '',
  545. major: (classification.tags.major || [])[0] || '',
  546. stage: (classification.tags.stage || [])[0] || '',
  547. subject: (classification.tags.subject || [])[0] || '',
  548. fitStatus: classification.fit.fitStatus || [],
  549. schoolAliases: classification.tags.schoolAliases || [],
  550. imageUrls,
  551. hasImage: imageUrls.length > 0,
  552. imageText: truncate(orderedAssets.map((a) => a.ocrText).filter(Boolean).join(' '), 400),
  553. privacyFindings: classification.compliance.privacyFindings || [],
  554. complianceBlockers: classification.compliance.complianceBlockers || [],
  555. reviewHint: classification.compliance.reviewHint,
  556. summaryLine: casePackageSummaryLine(classification, orderedAssets.length),
  557. submittedBy: options.submittedBy || '',
  558. // ---- 落库后由服务端决定的字段(此处只声明预期值,供 dry-run 展示)
  559. expected: { reviewStatus: 'pending', readyForUse: false },
  560. };
  561. // ---- 产出物
  562. const artifacts = {
  563. extracted: plan.artifacts.extracted,
  564. groupPlan: plan.artifacts.groupPlan,
  565. vision: path.join(outDir, 'vision.json'),
  566. materialAssets: path.join(outDir, 'material-assets.json'),
  567. casePackage: path.join(outDir, 'case-package.json'),
  568. report: path.join(outDir, 'report.md'),
  569. };
  570. writeJson(artifacts.vision, vision);
  571. writeJson(artifacts.materialAssets, { groupInfoList, materialAssets: orderedAssets });
  572. writeJson(artifacts.casePackage, casePackage);
  573. fs.writeFileSync(artifacts.report, renderReport({ plan, casePackage, vision, dictionaryChanges, warnings }), 'utf8');
  574. const blockers = casePackage.complianceBlockers || [];
  575. const canSubmit = plan.authorizationStatus === 'authorized' && blockers.length === 0 && orderedAssets.length > 0;
  576. return { plan, casePackage, vision, dictionaryChanges, warnings, artifacts, canSubmit };
  577. }
  578. function casePackageSummaryLine(classification, assetCount) {
  579. const fits = (classification.fit.fitStatus || []).join('/') || '未命中';
  580. const risks = (classification.compliance.riskFlags || []).join('/') || '无';
  581. return `${assetCount} 个素材;适用跟进状态:${fits};风险:${risks}`;
  582. }
  583. // ---------------------------------------------------------------------------
  584. // 报告
  585. // ---------------------------------------------------------------------------
  586. export function renderReport({ plan, casePackage, vision, dictionaryChanges, warnings }) {
  587. const lines = [];
  588. lines.push(`# 案例采集报告 · ${casePackage.runId}`);
  589. lines.push('');
  590. lines.push(`- 生成时间:${casePackage.generatedAt}`);
  591. lines.push(`- 来源类型:\`${casePackage.sourceType}\``);
  592. lines.push(`- 授权状态:\`${casePackage.authorizationStatus || '缺失'}\``);
  593. lines.push(`- 幂等键:\`${casePackage.idempotencyKey}\``);
  594. lines.push(`- 素材数:${casePackage.materialAssets.length}(分组 ${casePackage.groupInfoList.length} 组)`);
  595. lines.push(`- 图片理解:${vision.available ? `已启用(${vision.source}${vision.needsHostRead ? ',待宿主读图' : ''})` : '不可用'}`);
  596. lines.push('');
  597. lines.push('## 素材顺序(materialAssets)');
  598. lines.push('');
  599. lines.push('| order | groupId | kind | role | 标签 | 说明 |');
  600. lines.push('| --- | --- | --- | --- | --- | --- |');
  601. for (const asset of casePackage.materialAssets) {
  602. lines.push(`| ${asset.order} | ${asset.groupId} | ${asset.kind} | ${asset.role} | ${truncate(asset.label, 20)} | ${truncate(asset.description || asset.ocrText, 60)} |`);
  603. }
  604. lines.push('');
  605. lines.push('## 分组(九宫格阅读顺序:左上 → 右下)');
  606. lines.push('');
  607. for (const group of casePackage.groupInfoList) {
  608. lines.push(`- \`${group.groupId}\` ${group.layout} · ${group.count} 张 · \`${group.orderingRule}\``);
  609. }
  610. lines.push('');
  611. lines.push('## 标签');
  612. lines.push('');
  613. lines.push(`- 学校(归一):${casePackage.schoolCanonical || '未识别'}${casePackage.schoolAliases.length ? `(命中别名:${casePackage.schoolAliases.join('、')})` : ''}`);
  614. lines.push(`- 国家:${casePackage.country || '未识别'} · 专业:${casePackage.major || '未识别'} · 阶段:${casePackage.stage || '未识别'} · 科目:${casePackage.subject || '未识别'}`);
  615. lines.push(`- 亮点:${casePackage.highlightTypes.join('、') || '无'}`);
  616. lines.push(`- 场景:${casePackage.scenarioTags.join('、') || '无'}`);
  617. lines.push(`- 异议:${casePackage.objectionTags.join('、') || '无'}`);
  618. lines.push(`- 适用跟进状态:${(casePackage.fitStatus || []).join('、') || '未命中'}`);
  619. lines.push('');
  620. lines.push('## 合规判断');
  621. lines.push('');
  622. lines.push(`- riskFlags:${casePackage.riskFlags.join('、') || '无'}`);
  623. lines.push(`- privacyFindings:${casePackage.privacyFindings.length} 条(仅记录,本期不打码)`);
  624. for (const finding of casePackage.privacyFindings) {
  625. lines.push(` - ${finding.field} / ${finding.rule}:\`${finding.snippet}\``);
  626. }
  627. lines.push(`- 结论:${casePackage.complianceBlockers.length ? '不入库' : '可入库待审'}`);
  628. for (const blocker of casePackage.complianceBlockers) lines.push(` - ⛔ ${blocker}`);
  629. lines.push(`- 审核提示:${casePackage.reviewHint}`);
  630. lines.push('');
  631. lines.push('## 字典自进化');
  632. lines.push('');
  633. lines.push(`- 新增标签:${dictionaryChanges.tags.length ? dictionaryChanges.tags.map((t) => `${t.dimension}=${t.value}`).join('、') : '无'}`);
  634. lines.push(`- 新增别名:${dictionaryChanges.aliases.length ? dictionaryChanges.aliases.map((a) => `${a.aliasText}→${a.canonicalName}`).join('、') : '无'}`);
  635. lines.push('');
  636. if (warnings.length) {
  637. lines.push('## 警告');
  638. lines.push('');
  639. for (const warning of uniq(warnings)) lines.push(`- ⚠️ ${warning}`);
  640. lines.push('');
  641. }
  642. lines.push('## 下一步');
  643. lines.push('');
  644. lines.push('1. 打开 `material-assets.json` 核对顺序(九宫格从左到右、从上到下),需要调整用 `reorder`;');
  645. lines.push('2. 确认无误后执行 `submit --package case-package.json`,落到待审清单(reviewStatus=pending,不进公共素材库);');
  646. lines.push('3. 由负责人用 `caseApprove` 审核放行后才会 `readyForUse=true`、进检索结果。');
  647. return `${lines.join('\n')}\n`;
  648. }
  649. // ---------------------------------------------------------------------------
  650. // submit
  651. // ---------------------------------------------------------------------------
  652. export async function runSubmit(options = {}) {
  653. const packagePath = path.resolve(options.package);
  654. const casePackage = readJson(packagePath, null);
  655. if (!casePackage) throw new Error(`读不到案例包:${packagePath}`);
  656. const blockers = casePackage.complianceBlockers || [];
  657. if (String(casePackage.authorizationStatus || '').toLowerCase() !== 'authorized') {
  658. return {
  659. submitted: false,
  660. reason: `authorizationStatus=${casePackage.authorizationStatus || '缺失'},仅 authorized 才可入库(硬规则:无授权不入库)`,
  661. blockers: ['UNAUTHORIZED'],
  662. };
  663. }
  664. if (blockers.length) {
  665. return { submitted: false, reason: 'complianceBlockers 非空,禁止入库', blockers };
  666. }
  667. if (!casePackage.materialAssets || !casePackage.materialAssets.length) {
  668. return { submitted: false, reason: 'materialAssets 为空,无法构成案例', blockers: ['NO_MATERIAL'] };
  669. }
  670. const useDirect = options.direct !== false; // 默认允许回落到 Parse 直写
  671. try {
  672. const response = await submitCase(casePackage, { submittedBy: options.submittedBy, token: options.token });
  673. if (!response.ok) {
  674. // 云函数存在但业务报错(如未授权 403)→ 不回落直写,如实返回
  675. // 端点不提供函数服务(404/400 HTML,或网络不可达)→ 回落 Parse 直写
  676. if (useDirect && (response.endpointUnavailable || response.error.code === 'NETWORK_UNAVAILABLE' || /404|不可达|超时/.test(response.error.message || ''))) {
  677. const direct = await submitViaParse(casePackage, { submittedBy: options.submittedBy });
  678. return { submitted: true, ...direct, fallback: 'cloud-unavailable -> parse-direct' };
  679. }
  680. return { submitted: false, reason: `caseSubmit 返回错误:${response.error.code} ${response.error.message}`, error: response.error };
  681. }
  682. return {
  683. submitted: true,
  684. objectId: response.result.objectId,
  685. reviewStatus: response.result.reviewStatus || 'pending',
  686. readyForUse: response.result.readyForUse === true,
  687. via: 'cloud-function',
  688. };
  689. } catch (error) {
  690. if (error instanceof CloudError) {
  691. // 客户自有云未挂载函数运行时 → 走同契约的 Parse 直写归档(仍只进待审清单)
  692. if (useDirect) {
  693. try {
  694. const direct = await submitViaParse(casePackage, { submittedBy: options.submittedBy });
  695. return { submitted: true, ...direct, fallback: 'cloud-unavailable -> parse-direct' };
  696. } catch (directErr) {
  697. return { submitted: false, reason: `云函数不可达,Parse 直写也失败:${directErr.message}`, offline: true };
  698. }
  699. }
  700. return {
  701. submitted: false,
  702. reason: error.message,
  703. offline: true,
  704. hint: '未提交成功(网络不可达/未登录)。案例包已保存在本地,可直接重跑 submit;幂等键保证重复提交只产生一条。',
  705. };
  706. }
  707. throw error;
  708. }
  709. }
  710. // ---------------------------------------------------------------------------
  711. // reorder
  712. // ---------------------------------------------------------------------------
  713. export function runReorder(options = {}) {
  714. const assetsPath = path.resolve(options.materialAssets || options.package);
  715. const bundle = readJson(assetsPath, null);
  716. if (!bundle) throw new Error(`读不到素材文件:${assetsPath}`);
  717. const list = Array.isArray(bundle) ? bundle : bundle.materialAssets;
  718. if (!Array.isArray(list)) throw new Error('素材文件里没有 materialAssets 数组');
  719. let next;
  720. if (options.move) {
  721. const [from, to] = String(options.move).split(/[:>,-]/).map((v) => Number(v.trim()));
  722. if (!Number.isFinite(from) || !Number.isFinite(to)) throw new Error('--move 格式应为 from:to,如 3:1');
  723. next = reorderAssets(list, from, to);
  724. } else if (options.order) {
  725. // --order "3,1,2" 表示「新顺序 = 原来第 3 项、第 1 项、第 2 项」(按位置,不是按 order 值)
  726. const positions = String(options.order).split(/[,\s]+/).map((v) => Number(v.trim())).filter(Number.isFinite);
  727. if (positions.length !== list.length) throw new Error(`--order 需要 ${list.length} 个位置序号,收到 ${positions.length} 个`);
  728. const byPosition = sortAssets(list);
  729. next = positions.map((position, index) => {
  730. const picked = byPosition[position - 1];
  731. if (!picked) throw new Error(`--order 位置越界:${position}(共 ${byPosition.length} 项)`);
  732. return { ...picked, order: index + 1 };
  733. });
  734. } else {
  735. throw new Error('需要 --move "3:1" 或 --order "3,1,2"');
  736. }
  737. const out = Array.isArray(bundle)
  738. ? next
  739. : { ...bundle, materialAssets: next, reorderedAt: new Date().toISOString() };
  740. writeJson(assetsPath, out);
  741. return { file: assetsPath, materialAssets: next };
  742. }
  743. // ---------------------------------------------------------------------------
  744. // 字典
  745. // ---------------------------------------------------------------------------
  746. export function runDictionary(options = {}) {
  747. const dict = loadDictionary(options.dictPath);
  748. if (options.add) {
  749. const items = [];
  750. for (const raw of options.add) {
  751. const [dimension, value] = String(raw).split('=');
  752. items.push({ dimension: (dimension || '').trim(), value: (value || '').trim() });
  753. }
  754. const result = learnTags(dict, items);
  755. if (result.added.length) saveDictionary(dict);
  756. return { added: result.added, skipped: result.skipped, file: dict.__file };
  757. }
  758. if (options.alias) {
  759. const entries = [];
  760. for (const raw of options.alias) {
  761. const [aliasText, canonicalName, country] = String(raw).split('=');
  762. entries.push({ aliasText: (aliasText || '').trim(), canonicalName: (canonicalName || '').trim(), country: (country || '').trim() });
  763. }
  764. const result = learnAliases(dict, entries);
  765. if (result.added.length) saveDictionary(dict);
  766. return { added: result.added, file: dict.__file };
  767. }
  768. return {
  769. file: dict.__file,
  770. dimensions: Object.entries(dict.dimensions || {}).map(([key, dim]) => ({
  771. dimension: key,
  772. label: dim.label,
  773. count: Array.isArray(dim.values) ? dim.values.length : Array.isArray(dim.entries) ? dim.entries.length : 0,
  774. values: dim.values || undefined,
  775. })),
  776. learnedCount: (dict.__learned || []).length,
  777. };
  778. }
  779. // ---------------------------------------------------------------------------
  780. // selftest
  781. // ---------------------------------------------------------------------------
  782. export async function runSelftest(options = {}) {
  783. const results = [];
  784. const python = resolvePython();
  785. // ① office 解包(python 标准库)
  786. const officeScript = path.join(SKILL_ROOT, 'scripts', 'office_extract.py');
  787. const officeRun = spawnSync(python, [officeScript, '--selftest'], { encoding: 'utf8' });
  788. results.push({
  789. name: 'office_extract(docx/pptx 标准库解包)',
  790. ok: officeRun.status === 0,
  791. detail: truncate(officeRun.stdout || officeRun.stderr || '', 400),
  792. });
  793. // ② 分组 / 打标 / 合规:离线合成样例
  794. const tmpRoot = ensureDir(path.join(outputsDir(options.outDir), 'selftest', newRunId()));
  795. const pngPath = path.join(tmpRoot, 'grid-01.png');
  796. fs.writeFileSync(pngPath, makeTinyPng(400, 400));
  797. const gridItems = [0, 1, 2, 3, 4, 5, 6, 7, 8].map((i) => ({
  798. kind: 'image',
  799. localPath: path.join(tmpRoot, `grid-${String(i + 1).padStart(2, '0')}.png`),
  800. width: 400,
  801. height: 400,
  802. mtimeMs: 1_700_000_000_000 + i * 1000,
  803. ocrText: '帝国理工 计算机科学 考前冲刺 提分',
  804. description: '',
  805. label: `朋友圈素材 ${i + 1}`,
  806. }));
  807. const groups = groupMaterials(gridItems, {});
  808. const gridOk = groups.length === 1 && groups[0].count === 9 && groups[0].layout === '3x3'
  809. && groups[0].items.every((item, index) => item.order === index + 1);
  810. results.push({
  811. name: '九宫格分组与顺序(3x3,order 1..9)',
  812. ok: gridOk,
  813. detail: `分组数=${groups.length},布局=${groups[0] && groups[0].layout},顺序=${groups[0] && groups[0].items.map((i) => i.order).join(',')}`,
  814. });
  815. const separated = groupMaterials([
  816. { kind: 'image', localPath: path.join(tmpRoot, 'a1.png'), width: 400, height: 400, mtimeMs: 1, ocrText: '帝国理工 提分' },
  817. { kind: 'image', localPath: path.join(tmpRoot, 'a2.png'), width: 400, height: 400, mtimeMs: 2000, ocrText: '帝国理工 提分' },
  818. { kind: 'image', localPath: path.join(tmpRoot, 'b1.png'), width: 1600, height: 900, mtimeMs: 900_000_000, ocrText: '香港大学 申诉' },
  819. ], {});
  820. results.push({
  821. name: '跨批次不误并组(时间 + 尺寸 + 内容三重信号)',
  822. ok: separated.length === 2,
  823. detail: `分组数=${separated.length},各组成员=${separated.map((g) => g.count).join('+')}`,
  824. });
  825. const dict = loadDictionary(options.dictPath);
  826. const classified = classifyCase({
  827. dict,
  828. groups: [...groups, ...separated],
  829. visionResults: [],
  830. textBundle: { paragraphs: ['帝国理工 计算机科学 考前冲刺辅导案例,学生期末提分 18 分,家长很满意', '微信:student_demo 联系 13800000000'], notes: [], titles: [], transcript: '' },
  831. authorizationStatus: 'authorized',
  832. hints: {},
  833. });
  834. const classifyOk = classified.tags.schoolCanonical.includes('帝国理工')
  835. && classified.tags.highlightTypes.includes('提分')
  836. && classified.compliance.privacyFindings.length > 0
  837. && classified.compliance.authorizationOk === true;
  838. results.push({
  839. name: '打标 + 别名归一(IC→帝国理工)+ 隐私发现',
  840. ok: classifyOk,
  841. detail: `学校=${classified.tags.schoolCanonical.join('/')},亮点=${classified.tags.highlightTypes.join('/')},隐私=${classified.compliance.privacyFindings.map((f) => f.field).join('/')}`,
  842. });
  843. const complaint = classifyCase({
  844. dict,
  845. groups: [],
  846. visionResults: [],
  847. textBundle: { paragraphs: ['学生投诉退费纠纷,要求赔偿'], notes: [], titles: [], transcript: '' },
  848. authorizationStatus: 'pending',
  849. hints: {},
  850. });
  851. const riskOk = complaint.compliance.riskFlags.includes('COMPLAINT')
  852. && complaint.compliance.riskFlags.includes('UNAUTHORIZED')
  853. && complaint.compliance.complianceBlockers.length >= 2
  854. && complaint.compliance.authorizationOk === false;
  855. results.push({
  856. name: '合规判断(客诉/负面 → riskFlags;无授权 → 不入库)',
  857. ok: riskOk,
  858. detail: `riskFlags=${complaint.compliance.riskFlags.join('/')},blockers=${complaint.compliance.complianceBlockers.length}`,
  859. });
  860. const reorderOk = (() => {
  861. const moved = reorderAssets(sortAssets(groups[0].items).map((i) => ({ ...i, kind: 'image' })), 9, 1);
  862. return moved[0].order === 1 && moved.length === 9;
  863. })();
  864. results.push({ name: '素材顺序调整(reorder)', ok: reorderOk, detail: '把第 9 张移到第 1 位后重新编号 1..9' });
  865. const failed = results.filter((r) => !r.ok);
  866. return { ok: failed.length === 0, checked: results.length, failed: failed.length, results, workDir: tmpRoot };
  867. }
  868. /** 合成一个最小的合法 PNG(用于自检,不引用任何真实素材)。 */
  869. export function makeTinyPng(width = 1, height = 1) {
  870. const crcTable = (() => {
  871. const table = new Int32Array(256);
  872. for (let n = 0; n < 256; n++) {
  873. let c = n;
  874. for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1;
  875. table[n] = c;
  876. }
  877. return table;
  878. })();
  879. const crc32 = (buf) => {
  880. let c = -1;
  881. for (const byte of buf) c = crcTable[(c ^ byte) & 0xff] ^ (c >>> 8);
  882. return (c ^ -1) >>> 0;
  883. };
  884. const chunk = (type, data) => {
  885. const length = Buffer.alloc(4);
  886. length.writeUInt32BE(data.length, 0);
  887. const typeBuf = Buffer.from(type, 'ascii');
  888. const crc = Buffer.alloc(4);
  889. crc.writeUInt32BE(crc32(Buffer.concat([typeBuf, data])), 0);
  890. return Buffer.concat([length, typeBuf, data, crc]);
  891. };
  892. const ihdr = Buffer.alloc(13);
  893. ihdr.writeUInt32BE(width, 0);
  894. ihdr.writeUInt32BE(height, 4);
  895. ihdr[8] = 8; // bit depth
  896. ihdr[9] = 6; // RGBA
  897. const raw = Buffer.alloc((width * 4 + 1) * height);
  898. return Buffer.concat([
  899. Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
  900. chunk('IHDR', ihdr),
  901. chunk('IDAT', zlib.deflateSync(raw)),
  902. chunk('IEND', Buffer.alloc(0)),
  903. ]);
  904. }
  905. // ---------------------------------------------------------------------------
  906. // CLI
  907. // ---------------------------------------------------------------------------
  908. function parseCli(argv) {
  909. const command = argv[0] && !argv[0].startsWith('-') ? argv[0] : 'ingest';
  910. const rest = command === argv[0] ? argv.slice(1) : argv;
  911. const { values, positionals } = parseArgs({
  912. args: rest,
  913. options: {
  914. input: { type: 'string', multiple: true, default: [] },
  915. files: { type: 'string', multiple: true, default: [] },
  916. image: { type: 'string', multiple: true, default: [] },
  917. doc: { type: 'string', multiple: true, default: [] },
  918. deck: { type: 'string', multiple: true, default: [] },
  919. video: { type: 'string', multiple: true, default: [] },
  920. 'out-dir': { type: 'string' },
  921. 'out': { type: 'string' },
  922. 'source-type': { type: 'string' },
  923. 'source-ref': { type: 'string' },
  924. authorization: { type: 'string', default: 'pending' },
  925. title: { type: 'string' },
  926. summary: { type: 'string' },
  927. 'target-customer': { type: 'string' },
  928. 'usage-suggestion': { type: 'string' },
  929. 'school-canonical': { type: 'string' },
  930. 'product-line': { type: 'string' },
  931. major: { type: 'string' },
  932. stage: { type: 'string' },
  933. country: { type: 'string' },
  934. highlight: { type: 'string', multiple: true, default: [] },
  935. scenario: { type: 'string', multiple: true, default: [] },
  936. objection: { type: 'string', multiple: true, default: [] },
  937. 'idempotency-key': { type: 'string' },
  938. 'asset-base-url': { type: 'string' },
  939. upload: { type: 'boolean', default: true },
  940. 'no-upload': { type: 'boolean', default: false },
  941. 'submitted-by': { type: 'string' },
  942. 'vision-model': { type: 'string' },
  943. 'skip-vision': { type: 'boolean', default: false },
  944. 'skip-video': { type: 'boolean', default: false },
  945. 'frame-count': { type: 'string' },
  946. diarize: { type: 'boolean', default: false },
  947. 'no-learn': { type: 'boolean', default: false },
  948. 'dict-path': { type: 'string' },
  949. package: { type: 'string' },
  950. 'material-assets': { type: 'string' },
  951. move: { type: 'string' },
  952. order: { type: 'string' },
  953. 'no-direct': { type: 'boolean', default: false },
  954. 'probe-direct': { type: 'boolean', default: false },
  955. add: { type: 'string', multiple: true, default: [] },
  956. alias: { type: 'string', multiple: true, default: [] },
  957. out: { type: 'string' },
  958. json: { type: 'boolean', default: false },
  959. help: { type: 'boolean', default: false, short: 'h' },
  960. },
  961. allowPositionals: true,
  962. });
  963. return { command, values, positionals };
  964. }
  965. function collectInputs(values, positionals) {
  966. return [
  967. ...values.input,
  968. ...values.files,
  969. ...values.image,
  970. ...values.doc,
  971. ...values.deck,
  972. ...values.video,
  973. ...positionals,
  974. ];
  975. }
  976. async function cliMain(argv) {
  977. const { command, values, positionals } = parseCli(argv);
  978. if (values.help) {
  979. process.stdout.write(HELP_TEXT);
  980. return 0;
  981. }
  982. if (command === 'selftest') {
  983. const report = await runSelftest({ dictPath: values['dict-path'], outDir: values['out-dir'] });
  984. process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
  985. return report.ok ? 0 : 1;
  986. }
  987. if (command === 'dictionary') {
  988. const report = runDictionary({ add: values.add, alias: values.alias, dictPath: values['dict-path'] });
  989. process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
  990. return 0;
  991. }
  992. if (command === 'reorder') {
  993. const report = runReorder({
  994. materialAssets: values['material-assets'] || values.package,
  995. move: values.move,
  996. order: values.order,
  997. });
  998. process.stdout.write(`${JSON.stringify({ ok: true, file: report.file, order: report.materialAssets.map((a) => a.order) }, null, 2)}\n`);
  999. return 0;
  1000. }
  1001. if (command === 'submit') {
  1002. if (!values.package) {
  1003. process.stderr.write('需要 --package <case-package.json>\n');
  1004. return 2;
  1005. }
  1006. const report = await runSubmit({
  1007. package: values.package,
  1008. submittedBy: values['submitted-by'],
  1009. direct: !values['no-direct'],
  1010. });
  1011. process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
  1012. return report.submitted ? 0 : 4;
  1013. }
  1014. if (command === 'probe' || values['probe-direct']) {
  1015. const report = await probeParse();
  1016. process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
  1017. return report.ok ? 0 : 4;
  1018. }
  1019. // plan / ingest
  1020. const outDir = values['out-dir'] || values.out
  1021. || path.join(outputsDir(), `${values['source-ref'] ? `${slug(values['source-ref'])}-` : ''}${newRunId()}`);
  1022. const options = {
  1023. inputs: collectInputs(values, positionals),
  1024. outDir,
  1025. sourceType: values['source-type'],
  1026. sourceRef: values['source-ref'],
  1027. authorizationStatus: values.authorization,
  1028. title: values.title,
  1029. summary: values.summary,
  1030. targetCustomer: values['target-customer'],
  1031. usageSuggestion: values['usage-suggestion'],
  1032. idempotencyKey: values['idempotency-key'],
  1033. assetBaseUrl: values['asset-base-url'] || process.env.CASE_ASSET_BASE_URL || '',
  1034. // 默认开:真正上传拿公网 URL;--no-upload 关闭(离线自测用),此时回落到 --asset-base-url 派生
  1035. upload: values['no-upload'] ? false : values.upload !== false,
  1036. submittedBy: values['submitted-by'],
  1037. visionModel: values['vision-model'],
  1038. skipVision: values['skip-vision'],
  1039. skipVideo: values['skip-video'],
  1040. frameCount: values['frame-count'] ? Number(values['frame-count']) : undefined,
  1041. diarize: values.diarize,
  1042. noLearn: values['no-learn'],
  1043. dictPath: values['dict-path'],
  1044. hints: {
  1045. schoolCanonical: values['school-canonical'],
  1046. productLine: values['product-line'],
  1047. major: values.major,
  1048. stage: values.stage,
  1049. country: values.country,
  1050. highlightTypes: values.highlight,
  1051. scenarioTags: values.scenario,
  1052. objectionTags: values.objection,
  1053. },
  1054. };
  1055. if (!options.inputs.length) {
  1056. process.stderr.write('至少需要一个输入(位置参数或 --input / --image / --doc / --deck / --video)\n');
  1057. return 2;
  1058. }
  1059. if (command === 'plan') {
  1060. const plan = await runPlan(options);
  1061. process.stdout.write(`${JSON.stringify({
  1062. ok: true,
  1063. command: 'plan',
  1064. runId: plan.runId,
  1065. sourceType: plan.sourceType,
  1066. counts: plan.counts,
  1067. groups: plan.groups.map((g) => ({ groupId: g.groupId, layout: g.layout, count: g.count, orderingRule: g.orderingRule })),
  1068. warnings: plan.warnings,
  1069. outDir,
  1070. }, null, 2)}\n`);
  1071. return 0;
  1072. }
  1073. const report = await runIngest(options);
  1074. const summary = {
  1075. ok: true,
  1076. command: 'ingest',
  1077. runId: report.casePackage.runId,
  1078. sourceType: report.casePackage.sourceType,
  1079. idempotencyKey: report.casePackage.idempotencyKey,
  1080. materialAssets: report.casePackage.materialAssets.length,
  1081. groups: report.casePackage.groupInfoList,
  1082. title: report.casePackage.title,
  1083. tags: {
  1084. schoolCanonical: report.casePackage.schoolCanonical,
  1085. schoolAliases: report.casePackage.schoolAliases,
  1086. country: report.casePackage.country,
  1087. major: report.casePackage.major,
  1088. stage: report.casePackage.stage,
  1089. subject: report.casePackage.subject,
  1090. highlightTypes: report.casePackage.highlightTypes,
  1091. scenarioTags: report.casePackage.scenarioTags,
  1092. objectionTags: report.casePackage.objectionTags,
  1093. fitStatus: report.casePackage.fitStatus,
  1094. },
  1095. compliance: {
  1096. riskFlags: report.casePackage.riskFlags,
  1097. privacyFindings: report.casePackage.privacyFindings.length,
  1098. blockers: report.casePackage.complianceBlockers,
  1099. canSubmit: report.canSubmit,
  1100. expected: report.casePackage.expected,
  1101. },
  1102. dictionaryChanges: report.dictionaryChanges,
  1103. warnings: uniq(report.warnings),
  1104. artifacts: report.artifacts,
  1105. };
  1106. process.stdout.write(`${JSON.stringify(summary, null, 2)}\n`);
  1107. return 0;
  1108. }
  1109. function slug(value) {
  1110. return String(value).replace(/[^\w.-]+/g, '-').replace(/^-+|-+$/g, '').slice(0, 40);
  1111. }
  1112. const HELP_TEXT = `skill-case-get —— 案例采集 / 拆解 / 归档
  1113. 用法:
  1114. case-intake.mjs ingest [输入...] [选项] 拆解 + 打标,产出案例包(默认不提交)
  1115. case-intake.mjs plan [输入...] 只识别 + 提取 + 分组(不调模型)
  1116. case-intake.mjs submit --package <file> 提交到云函数 caseSubmit(待审清单)
  1117. case-intake.mjs reorder --material-assets <file> --move "3:1"
  1118. case-intake.mjs dictionary [--add highlightTypes=退费保障] [--alias "IC=帝国理工=英国"]
  1119. case-intake.mjs selftest
  1120. 输入(可混用,支持目录):
  1121. --image a.jpg --image b.jpg ... 连续多图 / 裸图 / 九宫格
  1122. --doc case.docx Word
  1123. --deck deck.pptx PPT
  1124. --video clip.mp4 视频(抽帧 + 转写)
  1125. --input <file|dir> 自动按扩展名分流
  1126. --files <file|dir> 同 --input(别名)
  1127. 采集选项:
  1128. --authorization authorized|pending|denied 授权状态(硬门:仅 authorized 可入库,默认 pending)
  1129. --source-ref <ref> 来源引用(不含 PII)
  1130. --source-type <t> image_group|document|presentation|mixed|single_image
  1131. --title / --summary / --target-customer / --usage-suggestion 覆盖自动抽取的案例字段
  1132. --school-canonical / --product-line / --major / --stage / --country
  1133. --highlight / --scenario / --objection 显式补标签(可重复)
  1134. --idempotency-key <key> 幂等键(默认按内容指纹生成)
  1135. --asset-base-url <base> 本地素材 URL 前缀(CDN / Parse Files / 对象存储目录)**回退用**
  1136. (或环境变量 CASE_ASSET_BASE_URL)
  1137. --upload 真正上传本地素材到对象存储,拿公网 URL(默认开)
  1138. --no-upload 关闭上传;url 只按 --asset-base-url 派生(离线自测用)
  1139. object key 规范:case-library/<YYYYMMDD>/<caseSlug>-<order>.<ext>
  1140. --out-dir <dir> 产物目录(默认 <cwd>/case-get-outputs/<runId>)
  1141. --out <dir> 同 --out-dir(别名)
  1142. --skip-vision / --skip-video / --no-learn 关闭图片理解 / 视频处理 / 字典自进化
  1143. --submitted-by <userId> 提交人
  1144. 提交选项:
  1145. --package <case-package.json> ingest 产出的案例包
  1146. --submitted-by <userId>
  1147. 产物(--out-dir):
  1148. extracted.json 文档文本 + 内嵌媒体
  1149. group-plan.json 分组与九宫格顺序
  1150. vision.json 每张图的 OCR / 描述 / 使用建议
  1151. material-assets.json materialAssets[](可编辑顺序)
  1152. case-package.json 云函数 caseSubmit 入参(逐字对齐共享契约)
  1153. report.md 人类可读报告
  1154. `;
  1155. const invokedDirectly = process.argv[1] && path.resolve(process.argv[1]).endsWith(path.join('scripts', 'case-intake.mjs'));
  1156. if (invokedDirectly) {
  1157. cliMain(process.argv.slice(2))
  1158. .then((code) => process.exit(code))
  1159. .catch((error) => {
  1160. process.stderr.write(`skill-case-get 失败:${error.message}\n`);
  1161. process.exit(1);
  1162. });
  1163. }
  1164. export { cliMain };