File size: 3,380 Bytes
a070805
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import type { Automation } from "#/types/automation";
import type { SkillInfo } from "#/types/settings";
import {
  getSkillScope,
  SKILL_SCOPE_ORDER,
  type SkillScope,
} from "#/utils/skill-scope";

export const CONVERSATION_OVERVIEW_PROJECT_SCOPE = {
  project: "project",
  all: "all",
} as const;

export type ConversationOverviewProjectScope =
  (typeof CONVERSATION_OVERVIEW_PROJECT_SCOPE)[keyof typeof CONVERSATION_OVERVIEW_PROJECT_SCOPE];

export function normalizeRepositoryName(
  repository: string | null | undefined,
): string | null {
  if (!repository?.trim()) {
    return null;
  }
  return repository
    .trim()
    .toLowerCase()
    .replace(/\.git$/, "");
}

export function isAutomationForRepository(
  automation: Pick<Automation, "repository">,
  repository: string | null | undefined,
): boolean {
  const target = normalizeRepositoryName(repository);
  const automationRepo = normalizeRepositoryName(automation.repository);
  if (!target || !automationRepo) {
    return false;
  }
  return automationRepo === target;
}

export function isSkillForProject(
  skill: SkillInfo,
  projectDir?: string | null,
): boolean {
  return getSkillScope(skill, projectDir) === "project";
}

export function filterAutomationsByProjectScope(
  automations: Automation[],
  scope: ConversationOverviewProjectScope,
  repository: string | null | undefined,
): Automation[] {
  if (scope === CONVERSATION_OVERVIEW_PROJECT_SCOPE.all) {
    return automations;
  }
  return automations.filter((automation) =>
    isAutomationForRepository(automation, repository),
  );
}

export function filterSkillsByProjectScope(
  skills: SkillInfo[],
  scope: ConversationOverviewProjectScope,
  projectDir?: string | null,
): SkillInfo[] {
  if (scope === CONVERSATION_OVERVIEW_PROJECT_SCOPE.all) {
    return skills;
  }
  return skills.filter((skill) => isSkillForProject(skill, projectDir));
}

export function sortAutomationsByProjectRelevance(
  automations: Automation[],
  repository: string | null | undefined,
): Automation[] {
  return [...automations].sort((left, right) => {
    const leftMatch = isAutomationForRepository(left, repository) ? 0 : 1;
    const rightMatch = isAutomationForRepository(right, repository) ? 0 : 1;
    if (leftMatch !== rightMatch) {
      return leftMatch - rightMatch;
    }
    return left.name.localeCompare(right.name);
  });
}

export function sortSkillsByProjectRelevance(
  skills: SkillInfo[],
  projectDir?: string | null,
): SkillInfo[] {
  return [...skills].sort((left, right) => {
    const leftScope = getSkillScope(left, projectDir);
    const rightScope = getSkillScope(right, projectDir);
    const leftOrder = SKILL_SCOPE_ORDER.indexOf(leftScope as SkillScope);
    const rightOrder = SKILL_SCOPE_ORDER.indexOf(rightScope as SkillScope);
    if (leftOrder !== rightOrder) {
      return leftOrder - rightOrder;
    }
    return left.name.localeCompare(right.name);
  });
}

export function countAutomationsForRepository(
  automations: readonly Automation[],
  repository: string | null | undefined,
): number {
  return automations.filter((automation) =>
    isAutomationForRepository(automation, repository),
  ).length;
}

export function countSkillsForProject(
  skills: readonly SkillInfo[],
  projectDir?: string | null,
): number {
  return skills.filter((skill) => isSkillForProject(skill, projectDir)).length;
}