@cratis/pi 7.165.3 → 7.165.4

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.
@@ -8,6 +8,7 @@ import { corpusRoot } from './corpusRoot.ts';
8
8
  import { frontmatter } from './frontmatter.ts';
9
9
  import { globToRegExp, universalGlobs } from './globs.ts';
10
10
  import type { ManagedRule } from './ManagedRule.ts';
11
+ import { selectedProfileIds } from './skillSelection.ts';
11
12
 
12
13
  function rulesRoot(cwd: string): string {
13
14
  const managedRoot = join(cwd, '.cratis', 'ai', 'rules');
@@ -25,7 +26,7 @@ function configuration(cwd: string): AiConfiguration | undefined {
25
26
  }
26
27
 
27
28
  /**
28
- * A profile-specific rule is kept only when the repository selects that profile. Repositories without
29
+ * A profile-specific rule is kept when the repository selects or composes that profile. Repositories without
29
30
  * a `.cratis/ai.json` keep every rule, matching the `@cratis/pi` package.
30
31
  */
31
32
  function matchesProfile(rule: ManagedRule, selected: AiConfiguration | undefined): boolean {
@@ -37,6 +38,23 @@ function matchesProfile(rule: ManagedRule, selected: AiConfiguration | undefined
37
38
  return true;
38
39
  }
39
40
 
41
+ interface ScopeKind {
42
+ named: (scope: string, name: string) => boolean;
43
+ satisfied: (selected: AiConfiguration) => boolean;
44
+ }
45
+
46
+ const hasLanguage = (language: string) => (selected: AiConfiguration) => (selected.languages ?? []).includes(language);
47
+
48
+ const scopeKinds: ScopeKind[] = [
49
+ { named: scope => scope.includes('.cs'), satisfied: hasLanguage('csharp') },
50
+ { named: (scope, name) => scope.includes('.ts') || name === 'rtk.md' || name.endsWith('/rtk.md'), satisfied: hasLanguage('typescript') },
51
+ { named: scope => scope.includes('.go') || scope.includes('go.mod') || scope.includes('go.sum'), satisfied: hasLanguage('go') },
52
+ { named: scope => scope.includes('.kt'), satisfied: hasLanguage('kotlin') },
53
+ { named: scope => scope.includes('.java'), satisfied: hasLanguage('java') },
54
+ { named: scope => scope.includes('.ex'), satisfied: hasLanguage('elixir') },
55
+ { named: scope => scope.includes('md'), satisfied: selected => (selected.profiles ?? []).includes('cratis/documentation') },
56
+ ];
57
+
40
58
  /**
41
59
  * The packaged `@cratis/pi` corpus holds every rule, so the languages and documentation a repository selected
42
60
  * decide which apply: a rule scoped to `.cs` files needs `csharp`, to `.ts` files needs `typescript`, and to
@@ -46,23 +64,10 @@ function matchesSelection(name: string, applyTo: string[], selected: AiConfigura
46
64
  const languages = selected?.languages ?? [];
47
65
  if (!selected || languages.length === 0) return true;
48
66
  const scope = applyTo.join(',');
49
- const needsCSharp = scope.includes('.cs');
50
- const needsTypeScript = scope.includes('.ts') || name === 'rtk.md' || name.endsWith('/rtk.md');
51
- const needsGo = scope.includes('.go') || scope.includes('go.mod') || scope.includes('go.sum');
52
- const needsKotlin = scope.includes('.kt');
53
- const needsJava = scope.includes('.java');
54
- const needsElixir = scope.includes('.ex');
55
- const needsDocumentation = scope.includes('md');
56
67
  // A scope that also names model files (`.play`) is not tied to one language, so it is always kept.
57
68
  if (scope.includes('.play')) return true;
58
- if (!needsCSharp && !needsTypeScript && !needsGo && !needsKotlin && !needsJava && !needsElixir && !needsDocumentation) return true;
59
- return (needsCSharp && languages.includes('csharp')) ||
60
- (needsTypeScript && languages.includes('typescript')) ||
61
- (needsGo && languages.includes('go')) ||
62
- (needsKotlin && languages.includes('kotlin')) ||
63
- (needsJava && languages.includes('java')) ||
64
- (needsElixir && languages.includes('elixir')) ||
65
- (needsDocumentation && (selected.profiles ?? []).includes('cratis/documentation'));
69
+ const named = scopeKinds.filter(kind => kind.named(scope, name));
70
+ return named.length === 0 || named.some(kind => kind.satisfied(selected));
66
71
  }
67
72
 
68
73
  /**
@@ -72,7 +77,8 @@ function matchesSelection(name: string, applyTo: string[], selected: AiConfigura
72
77
  */
73
78
  export function managedRules(cwd: string): ManagedRule[] {
74
79
  const root = rulesRoot(cwd);
75
- const selected = configuration(cwd);
80
+ const configured = configuration(cwd);
81
+ const selected = configured ? { ...configured, profiles: [...selectedProfileIds(configured)] } : undefined;
76
82
  const resolved = existsSync(join(cwd, '.cratis', 'ai', 'rules'));
77
83
  return readdirSync(root, { recursive: true, encoding: 'utf8' })
78
84
  .filter((entry): entry is string => entry.endsWith('.md'))
@@ -27,10 +27,36 @@ function catalog(): Profile[] | undefined {
27
27
  }
28
28
 
29
29
  function supportsLanguage(profile: Profile, languages: Set<string>): boolean {
30
- if (!profile.languages?.length) return true;
30
+ if (!languages.size || !profile.languages?.length) return true;
31
31
  return profile.languages.some(language => language === 'language-agnostic' || languages.has(language));
32
32
  }
33
33
 
34
+ function profileForId(profiles: Profile[], selection: { id: string; allowUnknown: boolean }): Profile {
35
+ const profile = profiles.find(candidate => candidate.id === selection.id);
36
+ if (profile) return profile;
37
+ if (!selection.allowUnknown) throw new Error(`Unknown Cratis AI profile '${selection.id}'.`);
38
+ return { id: selection.id };
39
+ }
40
+
41
+ function resolveProfiles(configuration: AiConfiguration, profiles: Profile[], allowUnknown: boolean): Set<string> {
42
+ const languages = new Set(configuration.languages ?? []);
43
+ const selected = new Set<string>();
44
+ const select = (id: string, explicitSelection: boolean): void => {
45
+ const profile = profileForId(profiles, { id, allowUnknown });
46
+ if (!explicitSelection && !supportsLanguage(profile, languages)) return;
47
+ if (selected.has(id)) return;
48
+ selected.add(id);
49
+ profile.composes?.forEach(child => select(child, false));
50
+ };
51
+ configuration.profiles?.forEach(profile => select(profile, true));
52
+ return selected;
53
+ }
54
+
55
+ /** Effective rule profiles, retaining explicit custom profile IDs when they are not in the catalog. */
56
+ export function selectedProfileIds(configuration: AiConfiguration): Set<string> {
57
+ return resolveProfiles(configuration, catalog() ?? [], true);
58
+ }
59
+
34
60
  /**
35
61
  * The skills the packaged `@cratis/pi` makes available for a repository, resolved from `.cratis/ai.json` and the
36
62
  * profile catalog exactly as `selectedSkillPaths` in `@cratis/pi` does (a repository without `ai.json` gets every
@@ -49,17 +75,7 @@ export function selectedSkillNames(cwd: string): string[] | undefined {
49
75
  return [...new Set(profiles.flatMap(profile => profile.availableTargets ?? []))].sort();
50
76
  }
51
77
  const configuration = JSON.parse(readFileSync(configurationPath, 'utf8')) as AiConfiguration;
52
- const languages = new Set(configuration.languages ?? []);
53
- const selected = new Set<string>();
54
- const select = (id: string, explicitSelection: boolean): void => {
55
- const profile = profiles.find(candidate => candidate.id === id);
56
- if (!profile) throw new Error(`Unknown Cratis AI profile '${id}'.`);
57
- if (!explicitSelection && !supportsLanguage(profile, languages)) return;
58
- if (selected.has(id)) return;
59
- selected.add(id);
60
- profile.composes?.forEach(child => select(child, false));
61
- };
62
- configuration.profiles?.forEach(profile => select(profile, true));
78
+ const selected = resolveProfiles(configuration, profiles, false);
63
79
  return [...new Set(profiles.filter(profile => selected.has(profile.id)).flatMap(profile => profile.availableTargets ?? []))].sort();
64
80
  } catch {
65
81
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "7.165.3",
3
+ "version": "7.165.4",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [
package/src/index.ts CHANGED
@@ -34,17 +34,23 @@ function allSkillPaths(): string[] {
34
34
  }
35
35
 
36
36
  function supportsLanguage(profile: Profile, languages: Set<string>): boolean {
37
- if (!profile.languages?.length) return true;
37
+ if (!languages.size || !profile.languages?.length) return true;
38
38
  return profile.languages.some(language => language === 'language-agnostic' || languages.has(language));
39
39
  }
40
40
 
41
+ function profileForId(selection: { id: string; allowUnknown: boolean }): Profile {
42
+ const profile = profiles.find(candidate => candidate.id === selection.id);
43
+ if (profile) return profile;
44
+ if (!selection.allowUnknown) throw new Error(`Unknown Cratis AI profile '${selection.id}'.`);
45
+ return { id: selection.id };
46
+ }
47
+
41
48
  /** The ids of the configured profiles and, transitively, the composed profiles that support the configured languages. */
42
- function selectedProfileIds(configuration: AiConfiguration): Set<string> {
49
+ function selectedProfileIds(configuration: AiConfiguration, allowUnknown = false): Set<string> {
43
50
  const languages = new Set(configuration.languages ?? []);
44
51
  const selected = new Set<string>();
45
52
  const select = (id: string, explicitSelection: boolean): void => {
46
- const profile = profiles.find(candidate => candidate.id === id);
47
- if (!profile) throw new Error(`Unknown Cratis AI profile '${id}'.`);
53
+ const profile = profileForId({ id, allowUnknown });
48
54
  if (!explicitSelection && !supportsLanguage(profile, languages)) return;
49
55
  if (selected.has(id)) return;
50
56
  selected.add(id);
@@ -101,13 +107,13 @@ const scopeKinds: ScopeKind[] = [
101
107
  { named: applyTo => applyTo.includes('md'), satisfied: configuration => (configuration.profiles ?? []).includes('cratis/documentation') },
102
108
  ];
103
109
 
104
- function ruleMatchesConfiguration(path: string, content: string, configuration: AiConfiguration): boolean {
105
- if (!profileMatchesSelection(frontmatterValue(content, 'profile'), configuration.profiles ?? [])) return false;
110
+ function ruleMatchesConfiguration(rule: { path: string; content: string }, configuration: AiConfiguration): boolean {
111
+ if (!profileMatchesSelection(frontmatterValue(rule.content, 'profile'), configuration.profiles ?? [])) return false;
106
112
  if ((configuration.languages ?? []).length === 0) return true;
107
- const applyTo = frontmatterValue(content, 'applyTo') ?? '';
113
+ const applyTo = frontmatterValue(rule.content, 'applyTo') ?? '';
108
114
  // A scope that also names model files (`.play`) is not tied to one language, so it is always kept.
109
115
  if (applyTo.includes('.play')) return true;
110
- const named = scopeKinds.filter(kind => kind.named(applyTo, path));
116
+ const named = scopeKinds.filter(kind => kind.named(applyTo, rule.path));
111
117
  return named.length === 0 || named.some(kind => kind.satisfied(configuration));
112
118
  }
113
119
 
@@ -127,11 +133,12 @@ function rules(cwd: string): string {
127
133
  const configuration = existsSync(configurationPath)
128
134
  ? JSON.parse(readFileSync(configurationPath, 'utf8')) as AiConfiguration
129
135
  : undefined;
136
+ const effective = configuration ? { ...configuration, profiles: [...selectedProfileIds(configuration, true)] } : undefined;
130
137
  return readdirSync(root, { recursive: true, encoding: 'utf8' })
131
138
  .filter((entry): entry is string => entry.endsWith('.md'))
132
139
  .sort()
133
140
  .map(entry => ({ path: join(root, entry), content: readFileSync(join(root, entry), 'utf8') }))
134
- .filter(rule => !configuration || ruleMatchesConfiguration(rule.path, rule.content, configuration))
141
+ .filter(rule => !effective || ruleMatchesConfiguration(rule, effective))
135
142
  .filter(rule => isUniversalRule(rule.content))
136
143
  .map(rule => rule.content)
137
144
  .join('\n\n');