@aswless_854771076/ai_short_studio_cli 0.1.155 → 0.1.157
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/commands/canvas/document/get.js +3 -3
- package/dist/commands/canvas/document/save.js +8 -2
- package/dist/commands/canvas/documents.js +1 -1
- package/dist/commands/canvas/workflow/status.js +1 -1
- package/dist/domain/canvas-schema-key.d.ts +2 -0
- package/dist/domain/canvas-schema-key.js +21 -0
- package/dist/domain/vvicat-api.js +1 -1
- package/package.json +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/SKILL.md +13 -13
- package/skills/using-vvicat-ai-short-studio-cli/references/commands/canvas.md +21 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/humanizer/SOURCE.md +9 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/humanizer/humanizer.md +408 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/SKILL.md +16 -30
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-character-images.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-character-profiles.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-drama-art-style.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-prop-assets.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/generate-scene-assets.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/manage-drama-art-styles.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/manage-drama-assets.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/modules/ideate-drama-from-market/prompts/market_inspiration.en.txt +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/modules/ideate-drama-from-market/prompts/market_inspiration.zh.txt +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/modules/plan-drama-production/prompts/production_plan.en.txt +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/assets/plan-drama-assets.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/codex-contracts.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/analyze-drama-source.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/define-drama-brief.md +1 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/design-drama-bible.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/humanizer.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/outline-drama-series.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/short-drama.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/write-drama-director-book.md +1 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/creation/write-drama-episode.md +1 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing/edit-deliver-drama.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing/edit-drama-timeline.md +4 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing/remotion-best-practices.md +4 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/editing-workflow.md +8 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/feature-completeness.md +25 -25
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/configure-generation-providers.md +5 -11
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/design-drama-audio.md +4 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/drama-generation-service.md +6 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/generate-drama-videos.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/generate-storyboard-images.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/monitor-drama-tasks.md +9 -7
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/transform-drama-media.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/write-drama-video-prompts.md +9 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/analyze-drama-market.md +8 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/ideate-drama-from-market/market-inspiration.schema.json +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/ideate-drama-from-market.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/README.md +59 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/analyze.mjs +517 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/analyze.test.mjs +354 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/juchacha-client.mjs +188 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/juchacha-client.test.mjs +218 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/normalize.mjs +458 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/normalize.test.mjs +236 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/report.mjs +69 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/store.mjs +341 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/store.test.mjs +302 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/module-map.json +43 -133
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/music-catalog-providers.md +3 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/manage-drama-projects.md +5 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/orchestrate-short-drama.md +7 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/recover-drama-pipeline.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/short-drama-skill-index.md +7 -7
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/operations/use-short-drama-studio.md +4 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/pipeline.md +40 -65
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/project-spec-v1.md +91 -131
- package/skills/using-vvicat-ai-short-studio-cli/references/{hypit/playbooks/craft → short-drama/prompt}/h3-prompt-writing.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/prompt/prompt-craft.md +185 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/prompt/seedance-prompting.md +64 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/prompt-skill-index.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/recreation/recreation-qa.md +87 -0
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/review/review-drama-script.md +2 -2
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/review/review-drama-shots.md +5 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/build-drama-storyboard.md +1 -3
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/design-fight-video.md +0 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/direct-blender-previz/direction-contract.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/direct-blender-previz.md +1 -1
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/generate-blender-previz.md +5 -10
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/plan-drama-production.md +2 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/storyboard/plan-shot-continuity.md +4 -4
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/usage-guide.md +28 -70
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/SKILL.md +0 -447
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/brief.md +0 -93
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/project-files.md +0 -246
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/reference-video.md +0 -266
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/script-and-time.md +0 -253
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/creation/transformations.md +0 -141
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/distribution.md +0 -176
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/local-tools.md +0 -411
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/model-and-provider.md +0 -275
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/environment/profile.md +0 -286
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/asset-and-shot-preproduction.md +0 -193
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/b-roll.md +0 -94
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/caption-tracking.md +0 -125
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/captions.md +0 -252
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/examples/conversation-images.md +0 -218
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/examples/image-direction.md +0 -123
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/generated-dependencies.md +0 -38
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/graphic-compositions.md +0 -85
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/image-direction.md +0 -95
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/motion-graphics.md +0 -68
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/recreation-policy.md +0 -51
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/screen-demonstrations.md +0 -51
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/shot-production-contracts.md +0 -161
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/sound-mix.md +0 -76
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/video-direction.md +0 -433
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/voice-and-performance.md +0 -224
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/craft/voice-direction.md +0 -99
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/narration-led-demo.md +0 -87
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/presenter-led-explainer.md +0 -160
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/ranking-listicle.md +0 -71
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/reference-recreation-canvas.md +0 -19
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/short-drama-workflow.md +0 -165
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/short-drama.md +0 -88
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/street-interview.md +0 -92
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/talking-head.md +0 -82
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/formats/two-person-podcast.md +0 -101
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/playbooks/index.md +0 -77
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/audio-presentation.md +0 -49
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/authoring.md +0 -228
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/browser-capture.md +0 -112
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/builds.md +0 -330
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/caption-authoring.md +0 -156
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/caption-presentation.md +0 -66
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/component-design.md +0 -217
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/component-sharing.md +0 -150
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/component-visuals.md +0 -152
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/detail.svrun +0 -8
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/direction.svs +0 -14
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/look.svs +0 -6
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/material.svrun +0 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/production.svml +0 -73
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/production.svrun +0 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/examples/visuals.ts +0 -62
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/fonts-and-text.md +0 -138
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/image-operations.md +0 -102
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/index.md +0 -73
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/media-presentation.md +0 -111
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/media.md +0 -246
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/performance.md +0 -114
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/prompt-kits.md +0 -165
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/rendering.md +0 -141
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/review.md +0 -211
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/runs.md +0 -151
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/script-syntax.md +0 -236
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/snapshots.md +0 -90
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/sound.md +0 -75
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/source-syntax.md +0 -163
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/spatial.md +0 -189
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/studio-companions.md +0 -186
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/studio.md +0 -249
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/system.md +0 -169
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/timeline.md +0 -74
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/timing.md +0 -96
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/track-authoring.md +0 -215
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/tracks.md +0 -67
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/video-downloads.md +0 -47
- package/skills/using-vvicat-ai-short-studio-cli/references/hypit/production/vocabulary.md +0 -119
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/README.md +0 -11
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/comfly-provider.md +0 -12
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/generation/seedance-community-skills.md +0 -23
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/recreation/publish-drama-references/litterbox.md +0 -5
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/runninghub-provider.md +0 -33
- package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/starrouter-provider.md +0 -53
- package/skills/using-vvicat-ai-short-studio-cli/references/video-model-routing.md +0 -10
- package/skills/using-vvicat-ai-short-studio-cli/references/video-prompt-spec.md +0 -32
- /package/skills/using-vvicat-ai-short-studio-cli/references/{short-drama/assets/modules/humanizer → humanizer}/LICENSE +0 -0
- /package/skills/using-vvicat-ai-short-studio-cli/references/{hypit/playbooks/craft → short-drama/prompt}/examples/h3-base-prompt-writing.md +0 -0
- /package/skills/using-vvicat-ai-short-studio-cli/references/{hypit/playbooks/craft → short-drama/prompt}/examples/h3-reference-prompt-writing.md +0 -0
package/skills/using-vvicat-ai-short-studio-cli/references/short-drama/market/scripts/analyze.mjs
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
|
|
3
|
+
const ALL_RANKING_TYPES = Object.freeze(['hot', 'motion', 'motion-ai', 'motion-comedy', 'douyin', 'kuaishou', 'hongguo', 'income'])
|
|
4
|
+
const PERSISTENCE_CAP_DAYS = 30
|
|
5
|
+
|
|
6
|
+
function isRecord(value) {
|
|
7
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function finiteNumber(value) {
|
|
11
|
+
return typeof value === 'number' && Number.isFinite(value) ? value : null
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function uniqueStrings(values) {
|
|
15
|
+
return [...new Set((Array.isArray(values) ? values : []).filter((value) => typeof value === 'string' && value))]
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function compareText(left, right) {
|
|
19
|
+
return String(left).localeCompare(String(right), 'zh-Hans-CN')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function clamp(value, minimum = 0, maximum = 1) {
|
|
23
|
+
return Math.min(maximum, Math.max(minimum, value))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function average(values, fallback = 0) {
|
|
27
|
+
const usable = values.filter((value) => finiteNumber(value) !== null)
|
|
28
|
+
return usable.length === 0 ? fallback : usable.reduce((sum, value) => sum + value, 0) / usable.length
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function averageWithCoverage(values) {
|
|
32
|
+
const usable = values.filter((value) => finiteNumber(value) !== null)
|
|
33
|
+
return {
|
|
34
|
+
value: usable.length === 0 ? null : usable.reduce((sum, value) => sum + value, 0) / usable.length,
|
|
35
|
+
coverage: values.length === 0 ? 0 : usable.length / values.length,
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function topicsOf(item) {
|
|
40
|
+
const topics = uniqueStrings(item?.topics)
|
|
41
|
+
return topics.length > 0 ? topics : ['未分类']
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function keyOf(item, index = 0) {
|
|
45
|
+
// 平台会为同一 playletId 生成不同的观测 key;分析层优先使用稳定的作品 ID,避免把同剧重复计入供给或公司份额。
|
|
46
|
+
if (item?.playletId !== null && item?.playletId !== undefined && typeof item.playletId !== 'object' && String(item.playletId)) return `playlet-id:${item.playletId}`
|
|
47
|
+
if (typeof item?.key === 'string' && item.key) return item.key
|
|
48
|
+
if (typeof item?.key === 'number' && Number.isFinite(item.key)) return String(item.key)
|
|
49
|
+
return `observation:${index}`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function itemSort(left, right) {
|
|
53
|
+
const leftRank = finiteNumber(left.item.ranking) ?? Number.POSITIVE_INFINITY
|
|
54
|
+
const rightRank = finiteNumber(right.item.ranking) ?? Number.POSITIVE_INFINITY
|
|
55
|
+
return leftRank - rightRank || compareText(left.key, right.key) || left.index - right.index
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function dedupeObservations(items) {
|
|
59
|
+
const selected = new Map()
|
|
60
|
+
for (const [index, item] of (Array.isArray(items) ? items : []).entries()) {
|
|
61
|
+
if (!isRecord(item)) continue
|
|
62
|
+
const key = keyOf(item, index)
|
|
63
|
+
const rankingType = typeof item.rankingType === 'string' ? item.rankingType : ''
|
|
64
|
+
const observationKey = `${key}\u0000${rankingType}`
|
|
65
|
+
const candidate = { item, key, index }
|
|
66
|
+
const current = selected.get(observationKey)
|
|
67
|
+
if (!current || itemSort(candidate, current) < 0) selected.set(observationKey, candidate)
|
|
68
|
+
}
|
|
69
|
+
return [...selected.values()]
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function evidenceReference(item, key, fallbackSnapshotId = null) {
|
|
73
|
+
return {
|
|
74
|
+
snapshot_id: item?.snapshotId ?? item?.snapshot_id ?? fallbackSnapshotId,
|
|
75
|
+
ranking_type: typeof item?.rankingType === 'string' ? item.rankingType : '',
|
|
76
|
+
playlet_id: item?.playletId ?? key,
|
|
77
|
+
key,
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function evidenceSort(left, right) {
|
|
82
|
+
return compareText(left.ranking_type, right.ranking_type)
|
|
83
|
+
|| compareText(left.key, right.key)
|
|
84
|
+
|| compareText(left.snapshot_id, right.snapshot_id)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function percentilesByRankingType(entries, field) {
|
|
88
|
+
const result = new Map()
|
|
89
|
+
const groups = new Map()
|
|
90
|
+
for (const entry of entries) {
|
|
91
|
+
const rankingType = typeof entry.item.rankingType === 'string' ? entry.item.rankingType : ''
|
|
92
|
+
if (!groups.has(rankingType)) groups.set(rankingType, [])
|
|
93
|
+
groups.get(rankingType).push(entry)
|
|
94
|
+
}
|
|
95
|
+
for (const group of groups.values()) {
|
|
96
|
+
const values = group.map((entry) => field === 'ranking'
|
|
97
|
+
? (finiteNumber(entry.item.ranking) === null ? null : -entry.item.ranking)
|
|
98
|
+
: finiteNumber(entry.item[field]))
|
|
99
|
+
const ranks = percentileRanks(values)
|
|
100
|
+
for (const [index, entry] of group.entries()) result.set(entry, ranks[index])
|
|
101
|
+
}
|
|
102
|
+
return result
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function sourceTagRatio(entries) {
|
|
106
|
+
if (entries.length === 0) return 0
|
|
107
|
+
return entries.filter(({ item }) => item?.provenance?.topicSource === 'source-tag').length / entries.length
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function confidenceFor({ supplyCount, platformCoverage, sourceRatio, metricCoverage }) {
|
|
111
|
+
if (supplyCount >= 5 && platformCoverage >= 0.6 && sourceRatio >= 0.8 && metricCoverage >= 0.8) return 'high'
|
|
112
|
+
if (supplyCount >= 2 && platformCoverage >= 0.3 && sourceRatio >= 0.5 && metricCoverage >= 0.5) return 'medium'
|
|
113
|
+
return 'low'
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function normalizedCompanies(value) {
|
|
117
|
+
if (typeof value === 'string' && value.trim()) return [value.trim()]
|
|
118
|
+
if (Array.isArray(value)) return uniqueStrings(value.flatMap((entry) => normalizedCompanies(entry)))
|
|
119
|
+
if (!isRecord(value)) return []
|
|
120
|
+
return uniqueStrings(Object.values(value).flatMap((entry) => normalizedCompanies(entry)))
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function filterValues(value) {
|
|
124
|
+
if (value === undefined || value === null || value === '') return []
|
|
125
|
+
return Array.isArray(value) ? value.filter((entry) => entry !== null && entry !== undefined).map(String) : [String(value)]
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function matchesFilter(item, filters) {
|
|
129
|
+
const matchOne = (field) => {
|
|
130
|
+
const expected = filterValues(filters[field])
|
|
131
|
+
return expected.length === 0 || expected.includes(String(item?.[field] ?? ''))
|
|
132
|
+
}
|
|
133
|
+
const expectedTopics = filterValues(filters.topic)
|
|
134
|
+
const topicMatches = expectedTopics.length === 0 || topicsOf(item).some((topic) => expectedTopics.includes(topic))
|
|
135
|
+
const expectedRankingTypes = filterValues(filters.rankingTypes)
|
|
136
|
+
const rankingMatches = expectedRankingTypes.length === 0 || expectedRankingTypes.includes(String(item?.rankingType ?? ''))
|
|
137
|
+
return matchOne('audience') && matchOne('era') && matchOne('format') && topicMatches && rankingMatches
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function appliedFilters(filters) {
|
|
141
|
+
const result = {}
|
|
142
|
+
for (const field of ['audience', 'era', 'format', 'topic', 'rankingTypes']) {
|
|
143
|
+
if (filters?.[field] !== undefined) result[field] = Array.isArray(filters[field]) ? [...filters[field]].sort(compareText) : filters[field]
|
|
144
|
+
}
|
|
145
|
+
return result
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function deterministicGeneratedAt(items, filters) {
|
|
149
|
+
const explicit = filters?.generatedAt ?? filters?.now ?? filters?.context?.now
|
|
150
|
+
if (explicit !== undefined && explicit !== null && String(explicit)) return String(explicit)
|
|
151
|
+
const observed = (Array.isArray(items) ? items : [])
|
|
152
|
+
.map((item) => typeof item?.observedAt === 'string' ? item.observedAt : null)
|
|
153
|
+
.filter(Boolean)
|
|
154
|
+
.sort()
|
|
155
|
+
return observed.at(-1) ?? '1970-01-01T00:00:00.000Z'
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function stableSerialize(value) {
|
|
159
|
+
if (Array.isArray(value)) return `[${value.map((entry) => stableSerialize(entry)).join(',')}]`
|
|
160
|
+
if (isRecord(value)) return `{${Object.keys(value).sort().map((key) => `${JSON.stringify(key)}:${stableSerialize(value[key])}`).join(',')}}`
|
|
161
|
+
if (value === undefined) return 'null'
|
|
162
|
+
return JSON.stringify(value)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function deterministicReportId(payload) {
|
|
166
|
+
// 报告文件以 ID 为存储键,因此摘要完整规范化 payload(不含 report_id),避免趋势或筛选变化被旧报告覆盖。
|
|
167
|
+
const digest = createHash('sha256').update(stableSerialize(payload)).digest('hex').slice(0, 16)
|
|
168
|
+
return `market-report-${digest}`
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* 将有限数值映射到 0..1;并列取平均名次,单项或全并列取中性 0.5,非法值保持 null。
|
|
173
|
+
*/
|
|
174
|
+
export function percentileRanks(values) {
|
|
175
|
+
const source = Array.isArray(values) ? values : []
|
|
176
|
+
const result = source.map(() => null)
|
|
177
|
+
const valid = source.flatMap((value, index) => finiteNumber(value) === null ? [] : [{ value, index }])
|
|
178
|
+
if (valid.length === 0) return result
|
|
179
|
+
const distinct = [...new Set(valid.map(({ value }) => value))].sort((left, right) => left - right)
|
|
180
|
+
if (distinct.length === 1) {
|
|
181
|
+
for (const { index } of valid) result[index] = 0.5
|
|
182
|
+
return result
|
|
183
|
+
}
|
|
184
|
+
const sorted = [...valid].sort((left, right) => left.value - right.value || left.index - right.index)
|
|
185
|
+
const positions = new Map()
|
|
186
|
+
for (let start = 0; start < sorted.length;) {
|
|
187
|
+
let end = start + 1
|
|
188
|
+
while (end < sorted.length && sorted[end].value === sorted[start].value) end += 1
|
|
189
|
+
positions.set(sorted[start].value, ((start + end - 1) / 2) / (sorted.length - 1))
|
|
190
|
+
start = end
|
|
191
|
+
}
|
|
192
|
+
for (const { value, index } of valid) result[index] = positions.get(value)
|
|
193
|
+
return result
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* 题材指标以作品 key 去重供给,但热度、增长和名次始终先在各榜内标准化,避免不同榜的原始值相加。
|
|
198
|
+
*/
|
|
199
|
+
export function topicMetrics(items, successfulRankingTypes) {
|
|
200
|
+
const observations = dedupeObservations(items)
|
|
201
|
+
const successful = uniqueStrings(successfulRankingTypes)
|
|
202
|
+
const rankPercentiles = percentilesByRankingType(observations, 'ranking')
|
|
203
|
+
const demandPercentiles = percentilesByRankingType(observations, 'heatValue')
|
|
204
|
+
const growthPercentiles = percentilesByRankingType(observations, 'growthValue')
|
|
205
|
+
const allKeys = new Set(observations.map(({ key }) => key))
|
|
206
|
+
const grouped = new Map()
|
|
207
|
+
for (const entry of observations) {
|
|
208
|
+
for (const topic of topicsOf(entry.item)) {
|
|
209
|
+
if (!grouped.has(topic)) grouped.set(topic, [])
|
|
210
|
+
grouped.get(topic).push(entry)
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return [...grouped.entries()].map(([topic, entries]) => {
|
|
214
|
+
const works = new Map()
|
|
215
|
+
for (const entry of entries) if (!works.has(entry.key)) works.set(entry.key, entry)
|
|
216
|
+
const supplyCount = works.size
|
|
217
|
+
const coveredTypes = new Set(entries.map(({ item }) => item.rankingType).filter(Boolean))
|
|
218
|
+
const platformCoverage = successful.length === 0 ? null : clamp(coveredTypes.size / successful.length)
|
|
219
|
+
const demand = averageWithCoverage(entries.map((entry) => demandPercentiles.get(entry)))
|
|
220
|
+
const growth = averageWithCoverage(entries.map((entry) => growthPercentiles.get(entry)))
|
|
221
|
+
const rank = averageWithCoverage(entries.map((entry) => rankPercentiles.get(entry)))
|
|
222
|
+
const newTitle = averageWithCoverage([...works.keys()].map((key) => {
|
|
223
|
+
const values = entries.filter((entry) => entry.key === key).map((entry) => entry.item.isNew).filter((value) => value === true || value === false)
|
|
224
|
+
return values.length === 0 ? null : (values.includes(true) ? 1 : 0)
|
|
225
|
+
}))
|
|
226
|
+
const persistence = averageWithCoverage(entries.map(({ item }) => {
|
|
227
|
+
const days = finiteNumber(item.persistenceDays)
|
|
228
|
+
return days === null ? null : clamp(days / PERSISTENCE_CAP_DAYS)
|
|
229
|
+
}))
|
|
230
|
+
const crowding = allKeys.size === 0 ? 0 : clamp(supplyCount / allKeys.size)
|
|
231
|
+
const positiveIndicators = [
|
|
232
|
+
[0.28, demand.value],
|
|
233
|
+
[0.22, growth.value],
|
|
234
|
+
[0.16, newTitle.value],
|
|
235
|
+
[0.18, platformCoverage],
|
|
236
|
+
[0.16, persistence.value],
|
|
237
|
+
].filter(([, value]) => value !== null)
|
|
238
|
+
const positiveWeight = positiveIndicators.reduce((sum, [weight]) => sum + weight, 0)
|
|
239
|
+
const opportunityScore = positiveWeight === 0 ? null : Number((100 * clamp(
|
|
240
|
+
positiveIndicators.reduce((sum, [weight, value]) => sum + weight * value, 0) / positiveWeight
|
|
241
|
+
- 0.20 * crowding,
|
|
242
|
+
)).toFixed(12))
|
|
243
|
+
const metricCoverage = average([demand.coverage, growth.coverage, newTitle.coverage, persistence.coverage])
|
|
244
|
+
const sourceRatio = sourceTagRatio(entries)
|
|
245
|
+
const rankingHeat = [...new Set(entries.map(({ item }) => item.rankingType).filter(Boolean))].sort(compareText).map((rankingType) => {
|
|
246
|
+
const matching = entries.filter(({ item }) => item.rankingType === rankingType)
|
|
247
|
+
return { ranking_type: rankingType, demand_strength: averageWithCoverage(matching.map((entry) => demandPercentiles.get(entry))).value, sample_count: matching.length }
|
|
248
|
+
})
|
|
249
|
+
return {
|
|
250
|
+
topic,
|
|
251
|
+
ranking_types: uniqueStrings(entries.map(({ item }) => item.rankingType)).sort(compareText),
|
|
252
|
+
audiences: uniqueStrings(entries.map(({ item }) => item.audience)).sort(compareText),
|
|
253
|
+
formats: uniqueStrings(entries.map(({ item }) => item.format)).sort(compareText),
|
|
254
|
+
eras: uniqueStrings(entries.map(({ item }) => item.era)).sort(compareText),
|
|
255
|
+
ranking_heat: rankingHeat,
|
|
256
|
+
supply_count: supplyCount,
|
|
257
|
+
rank_strength: rank.value,
|
|
258
|
+
demand_strength: demand.value,
|
|
259
|
+
growth_strength: growth.value,
|
|
260
|
+
new_title_rate: newTitle.value,
|
|
261
|
+
platform_coverage: platformCoverage,
|
|
262
|
+
persistence_strength: persistence.value,
|
|
263
|
+
crowding,
|
|
264
|
+
opportunity_score: opportunityScore,
|
|
265
|
+
metric_coverage: {
|
|
266
|
+
rank_strength: rank.coverage,
|
|
267
|
+
demand_strength: demand.coverage,
|
|
268
|
+
growth_strength: growth.coverage,
|
|
269
|
+
new_title_rate: newTitle.coverage,
|
|
270
|
+
platform_coverage: platformCoverage === null ? 0 : 1,
|
|
271
|
+
persistence_strength: persistence.coverage,
|
|
272
|
+
},
|
|
273
|
+
confidence: confidenceFor({ supplyCount, platformCoverage: platformCoverage ?? 0, sourceRatio, metricCoverage }),
|
|
274
|
+
confidence_detail: { supply_count: supplyCount, platform_coverage: platformCoverage ?? 0, source_tag_ratio: sourceRatio, metric_coverage: metricCoverage },
|
|
275
|
+
}
|
|
276
|
+
}).sort((left, right) => right.opportunity_score - left.opportunity_score || compareText(left.topic, right.topic))
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
export function platformMatrix(items, fallbackSnapshotId = null) {
|
|
280
|
+
const grouped = new Map()
|
|
281
|
+
for (const entry of dedupeObservations(items)) {
|
|
282
|
+
if (!grouped.has(entry.key)) grouped.set(entry.key, [])
|
|
283
|
+
grouped.get(entry.key).push(entry)
|
|
284
|
+
}
|
|
285
|
+
return [...grouped.entries()].map(([key, entries]) => {
|
|
286
|
+
const first = [...entries].sort(itemSort)[0].item
|
|
287
|
+
const rankings = entries.map(({ item }) => ({
|
|
288
|
+
ranking_type: item.rankingType ?? '',
|
|
289
|
+
ranking: finiteNumber(item.ranking),
|
|
290
|
+
heat_value: finiteNumber(item.heatValue),
|
|
291
|
+
observed_at: typeof item.observedAt === 'string' ? item.observedAt : null,
|
|
292
|
+
topic_source: item?.provenance?.topicSource ?? null,
|
|
293
|
+
}))
|
|
294
|
+
.sort((left, right) => compareText(left.ranking_type, right.ranking_type) || (left.ranking ?? Infinity) - (right.ranking ?? Infinity))
|
|
295
|
+
return {
|
|
296
|
+
key,
|
|
297
|
+
title: first.title ?? '',
|
|
298
|
+
topics: uniqueStrings(entries.flatMap(({ item }) => topicsOf(item))).sort(compareText),
|
|
299
|
+
topic_sources: uniqueStrings(entries.map(({ item }) => item?.provenance?.topicSource)).sort(compareText),
|
|
300
|
+
ranking_types: rankings.map(({ ranking_type }) => ranking_type),
|
|
301
|
+
platform_coverage: rankings.length,
|
|
302
|
+
rankings,
|
|
303
|
+
evidence: entries.map(({ item }) => evidenceReference(item, key, fallbackSnapshotId)).sort(evidenceSort),
|
|
304
|
+
}
|
|
305
|
+
}).sort((left, right) => right.platform_coverage - left.platform_coverage || compareText(left.key, right.key))
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function concentrationFor(items, selectCompanies) {
|
|
309
|
+
const works = new Map()
|
|
310
|
+
for (const entry of dedupeObservations(items)) {
|
|
311
|
+
if (!works.has(entry.key)) works.set(entry.key, new Set())
|
|
312
|
+
for (const company of selectCompanies(entry.item)) works.get(entry.key).add(company)
|
|
313
|
+
}
|
|
314
|
+
const attributedWorks = [...works.entries()].filter(([, companies]) => companies.size > 0)
|
|
315
|
+
const counts = new Map()
|
|
316
|
+
for (const [, companies] of attributedWorks) for (const company of companies) counts.set(company, (counts.get(company) ?? 0) + 1)
|
|
317
|
+
const companies = [...counts.entries()].map(([company, playletCount]) => ({ company, playlet_count: playletCount, share: attributedWorks.length === 0 ? 0 : playletCount / attributedWorks.length }))
|
|
318
|
+
.sort((left, right) => right.playlet_count - left.playlet_count || compareText(left.company, right.company))
|
|
319
|
+
const topThree = new Set(companies.slice(0, 3).map(({ company }) => company))
|
|
320
|
+
// 多角色归属会让公司份额相加超过 1,因此 CR3 取前三家公司覆盖作品的并集,保持可解释的 0..1 范围。
|
|
321
|
+
const cr3 = attributedWorks.length === 0 ? 0 : attributedWorks.filter(([, list]) => [...list].some((company) => topThree.has(company))).length / attributedWorks.length
|
|
322
|
+
return { total_with_company: attributedWorks.length, company_count: companies.length, cr3, companies }
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function companyConcentration(items) {
|
|
326
|
+
const aggregate = concentrationFor(items, (item) => normalizedCompanies(item.companies))
|
|
327
|
+
const roleNames = ['platform', 'contractor', 'copyrightHolder', 'producer']
|
|
328
|
+
return {
|
|
329
|
+
...aggregate,
|
|
330
|
+
by_role: Object.fromEntries(roleNames.map((role) => [role, concentrationFor(items, (item) => normalizedCompanies(item.companies?.[role]))])),
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function topicCooccurrence(items, fallbackSnapshotId) {
|
|
335
|
+
const groups = new Map()
|
|
336
|
+
for (const entry of dedupeObservations(items)) {
|
|
337
|
+
const topics = topicsOf(entry.item).filter((topic) => topic !== '未分类').sort(compareText)
|
|
338
|
+
for (let index = 0; index < topics.length; index += 1) {
|
|
339
|
+
for (let next = index + 1; next < topics.length; next += 1) {
|
|
340
|
+
const pair = [topics[index], topics[next]]
|
|
341
|
+
const key = pair.join('\u0000')
|
|
342
|
+
if (!groups.has(key)) groups.set(key, { topics: pair, works: new Map() })
|
|
343
|
+
if (!groups.get(key).works.has(entry.key)) groups.get(key).works.set(entry.key, entry)
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return [...groups.values()].map(({ topics, works }) => ({
|
|
348
|
+
topics,
|
|
349
|
+
cooccurrence_count: works.size,
|
|
350
|
+
evidence: [...works.values()].map(({ item, key }) => evidenceReference(item, key, fallbackSnapshotId)).sort(evidenceSort),
|
|
351
|
+
})).sort((left, right) => right.cooccurrence_count - left.cooccurrence_count || compareText(left.topics.join('、'), right.topics.join('、')))
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
function supplyDemandQuadrants(metrics, items, fallbackSnapshotId) {
|
|
355
|
+
const entries = dedupeObservations(items)
|
|
356
|
+
return metrics.map((metric) => {
|
|
357
|
+
const evidence = entries.filter(({ item }) => topicsOf(item).includes(metric.topic))
|
|
358
|
+
.map(({ item, key }) => evidenceReference(item, key, fallbackSnapshotId)).sort(evidenceSort)
|
|
359
|
+
const quadrant = metric.demand_strength === null
|
|
360
|
+
? 'unknown'
|
|
361
|
+
: `${metric.demand_strength >= 0.5 ? 'high-demand' : 'low-demand'}-${metric.crowding > 0.5 ? 'high-supply' : 'low-supply'}`
|
|
362
|
+
return {
|
|
363
|
+
topic: metric.topic,
|
|
364
|
+
quadrant,
|
|
365
|
+
demand_strength: metric.demand_strength,
|
|
366
|
+
supply_count: metric.supply_count,
|
|
367
|
+
evidence,
|
|
368
|
+
}
|
|
369
|
+
})
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function platformPreferences(items, fallbackSnapshotId) {
|
|
373
|
+
const platforms = new Map()
|
|
374
|
+
for (const entry of dedupeObservations(items)) {
|
|
375
|
+
const rankingType = entry.item.rankingType ?? ''
|
|
376
|
+
if (!platforms.has(rankingType)) platforms.set(rankingType, [])
|
|
377
|
+
platforms.get(rankingType).push(entry)
|
|
378
|
+
}
|
|
379
|
+
return [...platforms.entries()].map(([rankingType, entries]) => {
|
|
380
|
+
const topics = new Map()
|
|
381
|
+
for (const entry of entries) {
|
|
382
|
+
for (const topic of topicsOf(entry.item)) {
|
|
383
|
+
if (!topics.has(topic)) topics.set(topic, [])
|
|
384
|
+
topics.get(topic).push(entry)
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
const topicPreferences = [...topics.entries()].map(([topic, matching]) => ({
|
|
388
|
+
topic,
|
|
389
|
+
sample_count: matching.length,
|
|
390
|
+
share: entries.length === 0 ? 0 : matching.length / entries.length,
|
|
391
|
+
evidence: matching.map(({ item, key }) => evidenceReference(item, key, fallbackSnapshotId)).sort(evidenceSort),
|
|
392
|
+
})).sort((left, right) => right.sample_count - left.sample_count || compareText(left.topic, right.topic))
|
|
393
|
+
return { ranking_type: rankingType, topic_preferences: topicPreferences }
|
|
394
|
+
}).sort((left, right) => compareText(left.ranking_type, right.ranking_type))
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function snapshotShape(snapshot) {
|
|
398
|
+
if (Array.isArray(snapshot)) return { items: snapshot, successfulRankingTypes: uniqueStrings(snapshot.map((item) => item?.rankingType)).sort(compareText) }
|
|
399
|
+
const source = isRecord(snapshot) ? snapshot : {}
|
|
400
|
+
const items = Array.isArray(source.items) ? source.items : []
|
|
401
|
+
const successful = source.successfulRankingTypes ?? source.coverage?.successful_ranking_types ?? items.map((item) => item?.rankingType)
|
|
402
|
+
return { items, successfulRankingTypes: uniqueStrings(successful).sort(compareText) }
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function snapshotTime(snapshot) {
|
|
406
|
+
const source = isRecord(snapshot) ? snapshot : {}
|
|
407
|
+
const candidates = [source.observedAt, source.observed_at, source.retrievedAt, source.retrieved_at]
|
|
408
|
+
for (const item of Array.isArray(source.items) ? source.items : (Array.isArray(snapshot) ? snapshot : [])) candidates.push(item?.observedAt)
|
|
409
|
+
const timestamps = candidates.map((value) => Date.parse(value)).filter(Number.isFinite)
|
|
410
|
+
return timestamps.length === 0 ? null : Math.max(...timestamps)
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export function compareSnapshots(current, previous) {
|
|
414
|
+
const currentSnapshot = snapshotShape(current)
|
|
415
|
+
const previousSnapshot = snapshotShape(previous)
|
|
416
|
+
const comparable = currentSnapshot.items.length > 0
|
|
417
|
+
&& previousSnapshot.items.length > 0
|
|
418
|
+
&& currentSnapshot.successfulRankingTypes.length > 0
|
|
419
|
+
&& JSON.stringify(currentSnapshot.successfulRankingTypes) === JSON.stringify(previousSnapshot.successfulRankingTypes)
|
|
420
|
+
if (!comparable) return { comparable: false, trend: null }
|
|
421
|
+
const currentKeys = new Set(dedupeObservations(currentSnapshot.items).map(({ key }) => key))
|
|
422
|
+
const previousKeys = new Set(dedupeObservations(previousSnapshot.items).map(({ key }) => key))
|
|
423
|
+
const currentTime = snapshotTime(current)
|
|
424
|
+
const previousTime = snapshotTime(previous)
|
|
425
|
+
const elapsedHours = currentTime === null || previousTime === null ? null : Number((Math.abs(currentTime - previousTime) / 3_600_000).toFixed(6))
|
|
426
|
+
return {
|
|
427
|
+
comparable: true,
|
|
428
|
+
trend: {
|
|
429
|
+
sample_count_delta: currentSnapshot.items.length - previousSnapshot.items.length,
|
|
430
|
+
unique_title_delta: currentKeys.size - previousKeys.size,
|
|
431
|
+
new_key_count: [...currentKeys].filter((key) => !previousKeys.has(key)).length,
|
|
432
|
+
removed_key_count: [...previousKeys].filter((key) => !currentKeys.has(key)).length,
|
|
433
|
+
elapsed_hours: elapsedHours,
|
|
434
|
+
period_kind: elapsedHours === null ? 'unknown' : (elapsedHours < 24 ? 'instant' : 'daily'),
|
|
435
|
+
},
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function newTitleWatch(items, fallbackSnapshotId) {
|
|
440
|
+
const grouped = new Map()
|
|
441
|
+
for (const entry of dedupeObservations(items)) {
|
|
442
|
+
if (entry.item.isNew !== true) continue
|
|
443
|
+
if (!grouped.has(entry.key)) grouped.set(entry.key, [])
|
|
444
|
+
grouped.get(entry.key).push(entry)
|
|
445
|
+
}
|
|
446
|
+
return [...grouped.entries()].map(([key, entries]) => {
|
|
447
|
+
const first = [...entries].sort(itemSort)[0].item
|
|
448
|
+
const rankings = entries.map(({ item }) => ({ ranking_type: item.rankingType ?? '', ranking: finiteNumber(item.ranking) }))
|
|
449
|
+
.sort((left, right) => (left.ranking ?? Infinity) - (right.ranking ?? Infinity) || compareText(left.ranking_type, right.ranking_type))
|
|
450
|
+
return {
|
|
451
|
+
key,
|
|
452
|
+
title: first.title ?? '',
|
|
453
|
+
topics: uniqueStrings(entries.flatMap(({ item }) => topicsOf(item))).sort(compareText),
|
|
454
|
+
rankings,
|
|
455
|
+
evidence: entries.map(({ item }) => evidenceReference(item, key, fallbackSnapshotId)).sort(evidenceSort),
|
|
456
|
+
}
|
|
457
|
+
}).sort((left, right) => (left.rankings[0]?.ranking ?? Infinity) - (right.rankings[0]?.ranking ?? Infinity) || compareText(left.key, right.key))
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
export function analyzeMarket({ items, successfulRankingTypes, snapshotIds, previousItems = [], filters = {} } = {}) {
|
|
461
|
+
const sourceItems = Array.isArray(items) ? items : []
|
|
462
|
+
const sourcePreviousItems = Array.isArray(previousItems) ? previousItems : []
|
|
463
|
+
const filteredItems = sourceItems.filter((item) => isRecord(item) && matchesFilter(item, filters))
|
|
464
|
+
const filteredPreviousItems = sourcePreviousItems.filter((item) => isRecord(item) && matchesFilter(item, filters))
|
|
465
|
+
const successful = uniqueStrings(successfulRankingTypes).sort(compareText)
|
|
466
|
+
const analysisRankingTypes = uniqueStrings(filteredItems.map((item) => item.rankingType)).sort(compareText)
|
|
467
|
+
const suppliedSnapshotIds = uniqueStrings(snapshotIds)
|
|
468
|
+
// 入参首项是当前快照,排序仅服务于报告集合和稳定哈希;当前 observation 的证据不能改指向历史快照。
|
|
469
|
+
const currentSnapshotId = suppliedSnapshotIds[0] ?? null
|
|
470
|
+
const ids = [...suppliedSnapshotIds].sort(compareText)
|
|
471
|
+
const generatedAt = deterministicGeneratedAt(filteredItems, filters)
|
|
472
|
+
const metrics = topicMetrics(filteredItems, analysisRankingTypes)
|
|
473
|
+
// 采集口径属于快照元数据,筛选只改变分析样本,不能反推并缩窄历史成功榜单集合。
|
|
474
|
+
const previousSuccessful = filters?.previousSuccessfulRankingTypes ?? uniqueStrings(sourcePreviousItems.map((item) => item?.rankingType))
|
|
475
|
+
const hasTwoContentSnapshots = ids.length >= 2 && filteredItems.length > 0 && filteredPreviousItems.length > 0
|
|
476
|
+
const comparison = hasTwoContentSnapshots
|
|
477
|
+
? compareSnapshots({ items: filteredItems, successfulRankingTypes: successful }, { items: filteredPreviousItems, successfulRankingTypes: previousSuccessful })
|
|
478
|
+
: { comparable: false, trend: null }
|
|
479
|
+
const failed = ALL_RANKING_TYPES.filter((type) => !successful.includes(type))
|
|
480
|
+
const evidence = dedupeObservations(filteredItems)
|
|
481
|
+
.map(({ item, key }) => evidenceReference(item, key, currentSnapshotId))
|
|
482
|
+
.sort(evidenceSort)
|
|
483
|
+
const limitations = ['数据仅覆盖公开 Top 30 榜单样本,不能代表全量市场。']
|
|
484
|
+
if (ids.length <= 1) limitations.push('单次快照仅反映采集时点,不应直接视为趋势。')
|
|
485
|
+
if (!hasTwoContentSnapshots) limitations.push('趋势不可用:缺少两个同口径且有实际内容的快照。')
|
|
486
|
+
if (failed.length > 0) limitations.push(`榜单覆盖受限:${failed.join('、')} 未成功采集。`)
|
|
487
|
+
if (hasTwoContentSnapshots && !comparison.comparable) limitations.push('历史快照成功榜单集合不同,结果不可比,未生成趋势。')
|
|
488
|
+
const payload = {
|
|
489
|
+
schema_version: 'market-report.v2',
|
|
490
|
+
generated_at: generatedAt,
|
|
491
|
+
snapshot_ids: ids,
|
|
492
|
+
coverage: {
|
|
493
|
+
sample_count: filteredItems.length,
|
|
494
|
+
successful_ranking_types: successful,
|
|
495
|
+
analysis_ranking_types: analysisRankingTypes,
|
|
496
|
+
failed_ranking_types: failed,
|
|
497
|
+
low_confidence_topic_count: metrics.filter((metric) => metric.confidence === 'low').length,
|
|
498
|
+
},
|
|
499
|
+
filters: appliedFilters(filters),
|
|
500
|
+
summary: {
|
|
501
|
+
formula_version: 'topic-opportunity-v1',
|
|
502
|
+
methodology: '榜内稳健百分位聚合;持久度按 30 天截尾归一化;供给按作品 key 去重。',
|
|
503
|
+
topic_count: metrics.length,
|
|
504
|
+
trend: comparison.trend,
|
|
505
|
+
},
|
|
506
|
+
topic_metrics: metrics,
|
|
507
|
+
topic_cooccurrence: topicCooccurrence(filteredItems, currentSnapshotId),
|
|
508
|
+
supply_demand_quadrants: supplyDemandQuadrants(metrics, filteredItems, currentSnapshotId),
|
|
509
|
+
platform_preferences: platformPreferences(filteredItems, currentSnapshotId),
|
|
510
|
+
platform_matrix: platformMatrix(filteredItems, currentSnapshotId),
|
|
511
|
+
company_concentration: companyConcentration(filteredItems),
|
|
512
|
+
new_title_watch: newTitleWatch(filteredItems, currentSnapshotId),
|
|
513
|
+
evidence,
|
|
514
|
+
limitations,
|
|
515
|
+
}
|
|
516
|
+
return { schema_version: payload.schema_version, report_id: deterministicReportId(payload), ...payload }
|
|
517
|
+
}
|