Skip to content

Commit 8989120

Browse files
committed
feat: refactor rules generation to ai-context with skill output
Rename src/rules to src/ai-context and enhance the context generation: - Extract example configs to separate context.ts module with doc references - Generate both checkly.rules.md and skills/monitoring/SKILL.md - Add YAML frontmatter for skill metadata (stripped from rules output) - Support inline exampleConfig in addition to exampleConfigPath
1 parent d32f2e1 commit 8989120

File tree

7 files changed

+251
-205
lines changed

7 files changed

+251
-205
lines changed

packages/cli/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
"clean:gen": "rimraf ./gen",
1919
"clean": "npm run clean:dist && npm run clean:gen",
2020
"prepack": "npx oclif manifest",
21-
"generate:rules": "cross-env CHECKLY_SKIP_AUTH=1 CHECKLY_CLI_VERSION=99.0.0 ./bin/run import plan --root gen --debug-import-plan-input-file ./src/rules/rules.fixtures.json && ts-node ./scripts/compile-rules.ts",
21+
"generate:ai-context": "cross-env CHECKLY_SKIP_AUTH=1 CHECKLY_CLI_VERSION=99.0.0 ./bin/run import plan --root gen --debug-import-plan-input-file ./src/ai-context/context.fixtures.json && ts-node ./scripts/compile-ai-context.ts",
2222
"prepare:dist": "tsc --build",
23-
"prepare": "npm run clean && npm run prepare:dist && npm run generate:rules",
23+
"prepare": "npm run clean && npm run prepare:dist && npm run generate:ai-context",
2424
"test": "vitest",
2525
"test:e2e": "npm run prepare && cross-env NODE_CONFIG_DIR=./e2e/config vitest -c ./vitest.config.e2e.mts",
2626
"test:e2e:local": "cross-env CHECKLY_BASE_URL=http://localhost:3000 CHECKLY_ENV=local npm run test:e2e",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// ABOUTME: Compiles the context template with code examples into rules and skill outputs.
2+
// ABOUTME: Generates dist/ai-context/checkly.rules.md and skills/monitoring/SKILL.md
3+
4+
import { mkdir, readFile, writeFile } from 'fs/promises'
5+
import { join } from 'path'
6+
import { EXAMPLE_CONFIGS } from '../src/ai-context/context'
7+
8+
const EXAMPLES_DIR = join(__dirname, '../gen/')
9+
const CONTEXT_TEMPLATE_PATH = join(
10+
__dirname,
11+
'../src/ai-context/checkly.context.template.md',
12+
)
13+
const RULES_OUTPUT_DIR = join(__dirname, '../dist/ai-context')
14+
const SKILL_OUTPUT_DIR = join(__dirname, '../../../skills/monitoring')
15+
16+
function stripYamlFrontmatter (content: string): string {
17+
const frontmatterRegex = /^---\n[\s\S]*?\n---\n+/
18+
return content.replace(frontmatterRegex, '')
19+
}
20+
21+
async function writeOutput (content: string, dir: string, filename: string): Promise<void> {
22+
await mkdir(dir, { recursive: true })
23+
const outputPath = join(dir, filename)
24+
await writeFile(outputPath, content, 'utf8')
25+
// eslint-disable-next-line no-console
26+
console.log(`✅ Compiled to ${outputPath}`)
27+
}
28+
29+
async function compileContext () {
30+
try {
31+
// eslint-disable-next-line no-console
32+
console.log('📝 Compiling context template with examples...')
33+
34+
let content = await readFile(CONTEXT_TEMPLATE_PATH, 'utf8')
35+
const examples = await readExampleCode()
36+
37+
for (const example of examples) {
38+
content = content.replace(example.templateString, example.code)
39+
}
40+
41+
await writeOutput(content, SKILL_OUTPUT_DIR, 'SKILL.md')
42+
await writeOutput(
43+
stripYamlFrontmatter(content),
44+
RULES_OUTPUT_DIR,
45+
'checkly.rules.md',
46+
)
47+
} catch (error) {
48+
// eslint-disable-next-line no-console
49+
console.error('❌ Failed to compile context:', error)
50+
process.exit(1)
51+
}
52+
}
53+
54+
async function readExampleCode (): Promise<
55+
{ templateString: string, code: string }[]
56+
> {
57+
const examples: { templateString: string, code: string }[] = []
58+
59+
for (const config of Object.values(EXAMPLE_CONFIGS)) {
60+
let code: string | undefined
61+
62+
if (config.exampleConfig) {
63+
code = config.exampleConfig
64+
} else if (config.exampleConfigPath) {
65+
const filePath = join(EXAMPLES_DIR, config.exampleConfigPath)
66+
try {
67+
code = await readFile(filePath, 'utf8')
68+
} catch {
69+
// eslint-disable-next-line no-console
70+
console.warn(
71+
`Warning: Could not read example for ${config.templateString} from ${filePath}. It might not exist or be accessible.`,
72+
)
73+
}
74+
}
75+
76+
if (code) {
77+
const wrappedCode = `**Reference:** ${config.reference}\n\n\`\`\`typescript\n${code}\`\`\``
78+
examples.push({
79+
templateString: config.templateString,
80+
code: wrappedCode,
81+
})
82+
}
83+
}
84+
85+
return examples
86+
}
87+
88+
compileContext()

packages/cli/scripts/compile-rules.ts

Lines changed: 0 additions & 141 deletions
This file was deleted.

0 commit comments

Comments
 (0)