industry-trend-memory.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #!/usr/bin/env node
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { parseList } = require('./industry-trend-report');
  5. function parseArgs(argv) {
  6. const args = {};
  7. for (let i = 0; i < argv.length; i++) {
  8. const token = argv[i];
  9. if (!token.startsWith('--')) continue;
  10. const eq = token.indexOf('=');
  11. if (eq >= 0) {
  12. args[token.slice(2, eq)] = token.slice(eq + 1);
  13. } else {
  14. const key = token.slice(2);
  15. const next = argv[i + 1];
  16. if (next && !next.startsWith('--')) {
  17. args[key] = next;
  18. i++;
  19. } else {
  20. args[key] = true;
  21. }
  22. }
  23. }
  24. return args;
  25. }
  26. function readJson(filePath) {
  27. return fs.existsSync(filePath) ? JSON.parse(fs.readFileSync(filePath, 'utf8')) : {};
  28. }
  29. function cleanPreferenceItems(value) {
  30. return parseList(value)
  31. .map(item => item
  32. .replace(/^(这些|这类|这种|方向是|方向:|方向:)/, '')
  33. .replace(/(这些方向|这些内容|这类方向|这类内容|这种方向|这种内容|方向|内容)$/g, '')
  34. .trim())
  35. .filter(Boolean);
  36. }
  37. function main() {
  38. const args = parseArgs(process.argv.slice(2));
  39. const output = path.resolve(args.output || args.memory || 'memory/industry-trend-memory.json');
  40. const memory = {
  41. preferredSignals: [],
  42. blockedDirections: [],
  43. preferredOutput: [],
  44. highValueKeywords: [],
  45. ...readJson(output)
  46. };
  47. memory.preferredSignals = [...new Set([...(memory.preferredSignals || []), ...cleanPreferenceItems(args.keep || args.preferredSignals)])];
  48. memory.blockedDirections = [...new Set([...(memory.blockedDirections || []), ...cleanPreferenceItems(args.drop || args.blockedDirections)])];
  49. memory.preferredOutput = [...new Set([
  50. ...(memory.preferredOutput || []),
  51. ...parseList(args['output-preference'] || args.outputPreference || args.preferredOutput)
  52. ])];
  53. memory.highValueKeywords = [...new Set([...(memory.highValueKeywords || []), ...parseList(args.keywords)])];
  54. memory.updatedAt = new Date().toISOString();
  55. fs.mkdirSync(path.dirname(output), { recursive: true });
  56. fs.writeFileSync(output, JSON.stringify(memory, null, 2), 'utf8');
  57. console.log(`INDUSTRY_TREND_MEMORY_RESULT=${JSON.stringify({
  58. status: 'ok',
  59. memoryPath: output,
  60. memory,
  61. assistantMessage: '✅ 行业趋势偏好已保存。'
  62. })}`);
  63. }
  64. if (require.main === module) {
  65. try {
  66. main();
  67. } catch (error) {
  68. console.error(error && error.stack ? error.stack : String(error));
  69. process.exit(1);
  70. }
  71. }