Line data Source code
1 : /**
2 : * Store for template ranking scores.
3 : * Loads pre-computed usage scores from static JSON.
4 : * Internal ranks come from template.searchRank in index.json.
5 : * See docs/TEMPLATE_RANKING.md for details.
6 : */
7 :
8 : import { defineStore } from 'pinia'
9 : import { ref } from 'vue'
10 :
11 : import { searchRankBoost } from '@comfyorg/shared-frontend-utils/templateRanking'
12 :
13 2355 : export const useTemplateRankingStore = defineStore('templateRanking', () => {
14 24 : const largestUsageScore = ref<number>()
15 :
16 24 : const normalizeUsageScore = (usage: number): number => {
17 0 : const largest = largestUsageScore.value
18 0 : return largest ? usage / largest : 0
19 : }
20 :
21 : /**
22 : * Compute freshness score based on template date.
23 : * Returns 1.0 for brand new, decays to 0.1 over ~6 months.
24 : */
25 24 : const computeFreshness = (dateStr: string | undefined): number => {
26 0 : if (!dateStr) return 0.5 // Default for templates without dates
27 :
28 0 : const date = new Date(dateStr)
29 0 : if (isNaN(date.getTime())) return 0.5
30 :
31 0 : const daysSinceAdded = (Date.now() - date.getTime()) / (1000 * 60 * 60 * 24)
32 0 : return Math.max(0.1, 1.0 / (1 + daysSinceAdded / 90))
33 : }
34 :
35 : /**
36 : * Compute composite score for "recommended" sort.
37 : * Formula: usage × 0.5 + curation × 0.3 + freshness × 0.2
38 : */
39 24 : const computeDefaultScore = (
40 : dateStr: string | undefined,
41 : searchRank: number | undefined,
42 : usage: number = 0
43 : ): number => {
44 : // searchRankBoost is [-1, 1]; shift to [0, 1] so neutral stays the midpoint.
45 0 : const curation = (searchRankBoost(searchRank) + 1) / 2
46 0 : const freshness = computeFreshness(dateStr)
47 :
48 0 : return normalizeUsageScore(usage) * 0.5 + curation * 0.3 + freshness * 0.2
49 : }
50 :
51 24 : return {
52 : largestUsageScore,
53 : computeFreshness,
54 : computeDefaultScore
55 : }
56 : })
|