featureSuggestedPromptRotation.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import type { DraftSuggestedPromptItem } from "@/v4/draftSuggestedPromptItems.js";
  2. import { getRecommendedPromptPool } from "@/v4/featureSuggestedPrompts.js";
  3. type Mode = "office" | "coding";
  4. type Pane = { mode: Mode; cursor: number; items: DraftSuggestedPromptItem[] };
  5. const panes = new Map<string, Pane>();
  6. const listeners = new Set<() => void>();
  7. let revision = 0;
  8. function notify() {
  9. revision += 1;
  10. for (const listener of listeners) listener();
  11. }
  12. export function subscribeRecommendedPrompts(listener: () => void) {
  13. listeners.add(listener);
  14. return () => listeners.delete(listener);
  15. }
  16. export function getRecommendedPromptsRevision() {
  17. return revision;
  18. }
  19. export function registerRecommendedPromptPane(id: string, mode: Mode) {
  20. const existing = panes.get(id);
  21. if (existing?.mode === mode) return;
  22. const cursor = existing?.cursor ?? 0;
  23. const pane: Pane = { mode, cursor, items: [] };
  24. panes.set(id, pane);
  25. pane.items = selectForPane(
  26. getRecommendedPromptPool(mode === "office"),
  27. cursor,
  28. ...usedByOtherPanes(id, mode),
  29. );
  30. notify();
  31. }
  32. export function unregisterRecommendedPromptPane(id: string) {
  33. if (panes.delete(id)) notify();
  34. }
  35. export function advanceRecommendedPromptPane(id: string) {
  36. const pane = panes.get(id);
  37. if (!pane) return;
  38. // 每个候选都要有机会成为本批首项;固定跳三格会让避重时跳过的候选永远不可达。
  39. pane.cursor += 1;
  40. pane.items = selectForPane(
  41. getRecommendedPromptPool(pane.mode === "office"),
  42. pane.cursor,
  43. ...usedByOtherPanes(id, pane.mode),
  44. new Set(pane.items.map((item) => item.id)),
  45. );
  46. notify();
  47. }
  48. function usedByOtherPanes(id: string, mode: Mode): [Set<string>, Set<string>, Set<string>] {
  49. const ids = new Set<string>();
  50. const plugins = new Set<string>();
  51. const icons = new Set<string>();
  52. for (const [paneId, pane] of panes) {
  53. if (paneId === id || pane.mode !== mode) continue;
  54. for (const item of pane.items) {
  55. ids.add(item.id);
  56. icons.add(iconKey(item));
  57. for (const pluginId of pluginIds(item)) plugins.add(pluginId);
  58. }
  59. }
  60. return [ids, plugins, icons];
  61. }
  62. function pluginIds(item: DraftSuggestedPromptItem): string[] {
  63. const ids = new Set<string>();
  64. if (item.plugin) ids.add(item.plugin.stableId);
  65. for (const prompt of [item.prompt.cn, item.prompt.en]) {
  66. for (const match of prompt?.matchAll(/\(plugin:\/\/([\w.-]+@[\w.-]+)\)/g) ?? []) {
  67. if (match[1]) ids.add(match[1]);
  68. }
  69. }
  70. return [...ids];
  71. }
  72. function iconKey(item: DraftSuggestedPromptItem): string {
  73. return item.iconUrl ?? item.iconName ?? item.plugin?.stableId ?? item.id;
  74. }
  75. function selectForPane(
  76. pool: DraftSuggestedPromptItem[],
  77. cursor: number,
  78. globalIds: Set<string>,
  79. globalPlugins: Set<string>,
  80. globalIcons: Set<string>,
  81. previousIds: Set<string> = new Set(),
  82. ): DraftSuggestedPromptItem[] {
  83. if (pool.length === 0) return [];
  84. const selected: DraftSuggestedPromptItem[] = [];
  85. const localPlugins = new Set<string>();
  86. const localIcons = new Set<string>();
  87. const candidates = pool.map((_, index) => pool[(cursor + index) % pool.length]!);
  88. // 换一批优先全部换新;候选不足时才复用旧条目,并继续尽量保证同屏图标不同。
  89. for (const avoidPrevious of [true, false]) {
  90. for (const avoidGlobal of [true, false]) {
  91. for (const item of candidates) {
  92. if (selected.length >= Math.min(3, pool.length)) break;
  93. if (
  94. selected.some((chosen) => chosen.id === item.id) ||
  95. ((avoidGlobal || previousIds.size === 0) && globalIds.has(item.id)) ||
  96. (avoidPrevious && previousIds.has(item.id))
  97. )
  98. continue;
  99. const plugins = pluginIds(item);
  100. const icon = iconKey(item);
  101. if (localIcons.has(icon)) continue;
  102. if (plugins.some((id) => localPlugins.has(id))) continue;
  103. if (avoidGlobal && (globalIcons.has(icon) || plugins.some((id) => globalPlugins.has(id))))
  104. continue;
  105. selected.push(item);
  106. localIcons.add(icon);
  107. for (const id of plugins) localPlugins.add(id);
  108. }
  109. }
  110. }
  111. for (const avoidPrevious of [true, false]) {
  112. for (const item of candidates) {
  113. if (selected.length >= Math.min(3, pool.length)) break;
  114. if (avoidPrevious && previousIds.has(item.id)) continue;
  115. if (!selected.some((chosen) => chosen.id === item.id)) selected.push(item);
  116. }
  117. }
  118. for (const item of selected) {
  119. globalIds.add(item.id);
  120. globalIcons.add(iconKey(item));
  121. for (const id of pluginIds(item)) globalPlugins.add(id);
  122. }
  123. return selected;
  124. }
  125. export function getRecommendedPromptsForPane(id: string, mode: Mode): DraftSuggestedPromptItem[] {
  126. const pane = panes.get(id);
  127. if (pane?.mode === mode) return pane.items;
  128. return selectForPane(
  129. getRecommendedPromptPool(mode === "office"),
  130. 0,
  131. ...usedByOtherPanes(id, mode),
  132. );
  133. }