case-intake.mjs 49 KB

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