@cratis/pi 2.0.3 → 2.0.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.ts +38 -3
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cratis/pi",
3
- "version": "2.0.3",
3
+ "version": "2.0.5",
4
4
  "description": "Configuration-aware Cratis AI integration for Pi",
5
5
  "type": "module",
6
6
  "files": [
package/src/index.ts CHANGED
@@ -59,12 +59,47 @@ function isManagedInstallation(cwd: string): boolean {
59
59
  return existsSync(join(cwd, '.cratis', 'ai.manifest.json'));
60
60
  }
61
61
 
62
- function rules(): string {
62
+ function frontmatterValue(content: string, name: string): string | undefined {
63
+ if (!content.startsWith('---\n')) return undefined;
64
+ const end = content.indexOf('\n---\n', 4);
65
+ if (end < 0) return undefined;
66
+ const prefix = `${name}:`;
67
+ const line = content.slice(4, end).split('\n').find(candidate => candidate.startsWith(prefix));
68
+ return line?.slice(prefix.length).trim().replace(/^"|"$/g, '');
69
+ }
70
+
71
+ function ruleMatchesConfiguration(path: string, content: string, configuration: AiConfiguration): boolean {
72
+ const selectedProfiles = configuration.profiles ?? [];
73
+ const profile = frontmatterValue(content, 'profile');
74
+ const hasApplicationProfile = selectedProfiles.some(candidate => candidate.startsWith('cratis/application'));
75
+ const hasEngineeringProfile = selectedProfiles.some(candidate => candidate.startsWith('cratis/engineering'));
76
+ if (profile === 'application' && !hasApplicationProfile) return false;
77
+ if (profile === 'framework' && !hasEngineeringProfile) return false;
78
+
79
+ const languages = configuration.languages ?? [];
80
+ if (languages.length === 0) return true;
81
+ const applyTo = frontmatterValue(content, 'applyTo') ?? '';
82
+ const needsCSharp = applyTo.includes('.cs');
83
+ const needsTypeScript = applyTo.includes('.ts') || path.endsWith('/rtk.md');
84
+ const needsDocumentation = applyTo.includes('md');
85
+ if (!needsCSharp && !needsTypeScript && !needsDocumentation) return true;
86
+ return (needsCSharp && languages.includes('csharp')) ||
87
+ (needsTypeScript && languages.includes('typescript')) ||
88
+ (needsDocumentation && selectedProfiles.includes('cratis/documentation'));
89
+ }
90
+
91
+ function rules(cwd: string): string {
63
92
  const root = join(corpusRoot, 'rules');
93
+ const configurationPath = join(cwd, '.cratis', 'ai.json');
94
+ const configuration = existsSync(configurationPath)
95
+ ? JSON.parse(readFileSync(configurationPath, 'utf8')) as AiConfiguration
96
+ : undefined;
64
97
  return readdirSync(root, { recursive: true, encoding: 'utf8' })
65
98
  .filter((entry): entry is string => entry.endsWith('.md'))
66
99
  .sort()
67
- .map(entry => readFileSync(join(root, entry), 'utf8'))
100
+ .map(entry => ({ path: join(root, entry), content: readFileSync(join(root, entry), 'utf8') }))
101
+ .filter(rule => !configuration || ruleMatchesConfiguration(rule.path, rule.content, configuration))
102
+ .map(rule => rule.content)
68
103
  .join('\n\n');
69
104
  }
70
105
 
@@ -76,5 +111,5 @@ export default function (pi: ExtensionAPI): void {
76
111
  }));
77
112
  pi.on('before_agent_start', (event, context) => isManagedInstallation(context.cwd)
78
113
  ? undefined
79
- : ({ systemPrompt: `${event.systemPrompt}\n\n${rules()}` }));
114
+ : ({ systemPrompt: `${event.systemPrompt}\n\n${rules(context.cwd)}` }));
80
115
  }