@intentius/chant 0.0.18 → 0.0.24
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.
- package/bin/chant +4 -1
- package/package.json +20 -1
- package/src/build.test.ts +4 -2
- package/src/build.ts +3 -0
- package/src/builder.test.ts +3 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/astro.config.mjs +0 -3
- package/src/cli/commands/build.ts +5 -12
- package/src/cli/commands/diff.test.ts +2 -1
- package/src/cli/commands/diff.ts +2 -1
- package/src/cli/commands/init-lexicon/templates/codegen.ts +188 -0
- package/src/cli/commands/init-lexicon/templates/docs.ts +81 -0
- package/src/cli/commands/init-lexicon/templates/examples.ts +35 -0
- package/src/cli/commands/init-lexicon/templates/lint.ts +30 -0
- package/src/cli/commands/init-lexicon/templates/lsp.ts +39 -0
- package/src/cli/commands/init-lexicon/templates/plugin.ts +110 -0
- package/src/cli/commands/init-lexicon/templates/project.ts +182 -0
- package/src/cli/commands/init-lexicon/templates/spec.ts +57 -0
- package/src/cli/commands/init-lexicon/templates/tests.ts +70 -0
- package/src/cli/commands/init-lexicon.test.ts +0 -9
- package/src/cli/commands/init-lexicon.ts +12 -868
- package/src/cli/commands/init.ts +2 -20
- package/src/cli/conflict-check.test.ts +43 -0
- package/src/cli/handlers/build.ts +3 -3
- package/src/cli/handlers/lint.ts +2 -2
- package/src/cli/handlers/spell.ts +396 -0
- package/src/cli/handlers/state.ts +230 -0
- package/src/cli/lsp/server.test.ts +4 -0
- package/src/cli/main.ts +37 -3
- package/src/cli/mcp/resource-handlers.ts +227 -0
- package/src/cli/mcp/server.test.ts +13 -9
- package/src/cli/mcp/server.ts +24 -199
- package/src/cli/mcp/state-tools.ts +138 -0
- package/src/cli/mcp/tools/build.ts +2 -1
- package/src/cli/mcp/types.ts +45 -0
- package/src/cli/plugins.ts +1 -1
- package/src/cli/reporters/stylish.test.ts +2 -2
- package/src/cli/reporters/stylish.ts +1 -1
- package/src/codegen/docs-file-markers.ts +69 -0
- package/src/codegen/docs-rule-scanning.ts +159 -0
- package/src/codegen/docs-sections.ts +159 -0
- package/src/codegen/docs-sidebar.ts +56 -0
- package/src/codegen/docs-types.ts +79 -0
- package/src/codegen/docs.ts +9 -495
- package/src/composite.test.ts +76 -1
- package/src/composite.ts +37 -0
- package/src/config.ts +4 -0
- package/src/declarable.test.ts +2 -1
- package/src/declarable.ts +1 -1
- package/src/discovery/collect.test.ts +34 -0
- package/src/discovery/collect.ts +12 -0
- package/src/discovery/graph.test.ts +40 -0
- package/src/discovery/import.test.ts +5 -5
- package/src/discovery/resolve.test.ts +20 -0
- package/src/discovery/resolve.ts +2 -2
- package/src/index.ts +2 -0
- package/src/lexicon-plugin-helpers.ts +130 -0
- package/src/lexicon.ts +24 -0
- package/src/lint/rule-options.test.ts +3 -3
- package/src/lint/rule-registry.test.ts +1 -1
- package/src/lint/rules/composite-scope.ts +1 -1
- package/src/serializer-walker.ts +2 -1
- package/src/spell/discovery.ts +183 -0
- package/src/spell/index.ts +3 -0
- package/src/spell/prompt.ts +133 -0
- package/src/spell/types.ts +89 -0
- package/src/state/digest.ts +88 -0
- package/src/state/git.ts +317 -0
- package/src/state/index.ts +4 -0
- package/src/state/snapshot.ts +179 -0
- package/src/state/types.ts +59 -0
- package/src/toml-emit.ts +182 -0
- package/src/toml-parse.ts +370 -0
- package/src/toml-utils.ts +60 -0
- package/src/toml.ts +5 -602
- package/src/types.ts +2 -1
- package/src/utils.test.ts +16 -3
- package/src/utils.ts +31 -1
- package/src/validation.test.ts +11 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content/docs/getting-started.mdx +0 -6
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content/docs/lint-rules.mdx +0 -6
- package/src/cli/commands/__fixtures__/init-lexicon-output/docs/src/content/docs/serialization.mdx +0 -6
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/actions/.gitkeep +0 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/composites/.gitkeep +0 -0
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/coverage.ts +0 -11
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/import/generator.ts +0 -10
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/import/parser.ts +0 -10
- package/src/cli/commands/__fixtures__/init-lexicon-output/src/lint/post-synth/.gitkeep +0 -0
package/src/cli/commands/init.ts
CHANGED
|
@@ -3,7 +3,6 @@ import { join, resolve, dirname } from "path";
|
|
|
3
3
|
import { fileURLToPath } from "url";
|
|
4
4
|
import { homedir } from "os";
|
|
5
5
|
import { createInterface } from "readline";
|
|
6
|
-
import { z } from "zod";
|
|
7
6
|
import { formatSuccess, formatWarning } from "../format";
|
|
8
7
|
import { loadPlugin } from "../plugins";
|
|
9
8
|
|
|
@@ -18,17 +17,6 @@ function getChantVersion(): string {
|
|
|
18
17
|
}
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
/**
|
|
22
|
-
* Schema for validating generated package.json — catches template bugs early.
|
|
23
|
-
*/
|
|
24
|
-
const GeneratedPackageJsonSchema = z.object({
|
|
25
|
-
name: z.string().min(1),
|
|
26
|
-
version: z.string(),
|
|
27
|
-
type: z.literal("module"),
|
|
28
|
-
scripts: z.record(z.string(), z.string()),
|
|
29
|
-
dependencies: z.record(z.string(), z.string()),
|
|
30
|
-
});
|
|
31
|
-
|
|
32
20
|
/**
|
|
33
21
|
* Init command options
|
|
34
22
|
*/
|
|
@@ -128,12 +116,6 @@ function generatePackageJson(lexicon: string, extraScripts?: Record<string, stri
|
|
|
128
116
|
},
|
|
129
117
|
};
|
|
130
118
|
|
|
131
|
-
// Validate generated output to catch template bugs
|
|
132
|
-
const result = GeneratedPackageJsonSchema.safeParse(pkg);
|
|
133
|
-
if (!result.success) {
|
|
134
|
-
throw new Error(`Bug: generated package.json is invalid: ${result.error.issues[0].message}`);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
119
|
return JSON.stringify(pkg, null, 2);
|
|
138
120
|
}
|
|
139
121
|
|
|
@@ -240,7 +222,7 @@ export interface ChantConfig {
|
|
|
240
222
|
/**
|
|
241
223
|
* Generate MCP config
|
|
242
224
|
*/
|
|
243
|
-
function generateMcpConfig(
|
|
225
|
+
function generateMcpConfig(pm: "bun" | "npm"): string {
|
|
244
226
|
const config = {
|
|
245
227
|
mcpServers: {
|
|
246
228
|
chant: {
|
|
@@ -454,7 +436,7 @@ export async function initCommand(options: InitOptions): Promise<InitResult> {
|
|
|
454
436
|
|
|
455
437
|
writeIfNotExists(
|
|
456
438
|
join(mcpDir, "mcp.json"),
|
|
457
|
-
generateMcpConfig(
|
|
439
|
+
generateMcpConfig(detectPackageManager(targetDir)),
|
|
458
440
|
`~/.${ide === "generic" ? "config/mcp" : ide}/mcp.json`,
|
|
459
441
|
createdFiles,
|
|
460
442
|
warnings,
|
|
@@ -252,3 +252,46 @@ describe("checkConflicts", () => {
|
|
|
252
252
|
expect(report.conflicts[0].plugins).toEqual(["aws", "gcp", "azure"]);
|
|
253
253
|
});
|
|
254
254
|
});
|
|
255
|
+
|
|
256
|
+
// ---------------------------------------------------------------------------
|
|
257
|
+
// Cross-lexicon skill naming consistency
|
|
258
|
+
// ---------------------------------------------------------------------------
|
|
259
|
+
|
|
260
|
+
describe("skill naming consistency", () => {
|
|
261
|
+
test("all skill names match chant-{lexicon}(-{topic})* pattern", async () => {
|
|
262
|
+
const { readdirSync, readFileSync } = await import("fs");
|
|
263
|
+
const { join } = await import("path");
|
|
264
|
+
const lexiconsDir = join(import.meta.dir, "../../../../lexicons");
|
|
265
|
+
const lexiconNames = readdirSync(lexiconsDir, { withFileTypes: true })
|
|
266
|
+
.filter((d) => d.isDirectory())
|
|
267
|
+
.map((d) => d.name);
|
|
268
|
+
|
|
269
|
+
const violations: string[] = [];
|
|
270
|
+
const namePattern = /^chant-[a-z0-9]+(-[a-z0-9]+)*$/;
|
|
271
|
+
|
|
272
|
+
for (const lex of lexiconNames) {
|
|
273
|
+
const skillsDir = join(lexiconsDir, lex, "src", "skills");
|
|
274
|
+
let files: string[];
|
|
275
|
+
try {
|
|
276
|
+
files = readdirSync(skillsDir).filter((f: string) => f.endsWith(".md"));
|
|
277
|
+
} catch {
|
|
278
|
+
continue; // no skills dir
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
for (const file of files) {
|
|
282
|
+
const skillName = file.replace(/\.md$/, "");
|
|
283
|
+
if (!namePattern.test(skillName)) {
|
|
284
|
+
violations.push(`${lex}: ${file} → name "${skillName}" does not match pattern`);
|
|
285
|
+
}
|
|
286
|
+
// Verify file name starts with chant-{lexicon}
|
|
287
|
+
if (!skillName.startsWith(`chant-${lex}`)) {
|
|
288
|
+
// Allow k8s cross-provider skills (chant-k8s-eks, chant-k8s-aks, chant-k8s-gke)
|
|
289
|
+
// which are valid as they belong to the k8s lexicon
|
|
290
|
+
violations.push(`${lex}: ${file} → name should start with "chant-${lex}"`);
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
expect(violations).toEqual([]);
|
|
296
|
+
});
|
|
297
|
+
});
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { buildCommand, buildCommandWatch, printErrors, printWarnings } from "../commands/build";
|
|
2
|
-
import { formatInfo } from "../format";
|
|
2
|
+
import { formatError, formatInfo } from "../format";
|
|
3
3
|
import type { CommandContext } from "../registry";
|
|
4
4
|
|
|
5
5
|
export async function runBuild(ctx: CommandContext): Promise<number> {
|
|
@@ -10,14 +10,14 @@ export async function runBuild(ctx: CommandContext): Promise<number> {
|
|
|
10
10
|
if (args.lexicon) {
|
|
11
11
|
serializers = serializers.filter((s) => s.name === args.lexicon);
|
|
12
12
|
if (serializers.length === 0) {
|
|
13
|
-
console.error(`No serializer found for lexicon "${args.lexicon}". Available: ${ctx.serializers.map((s) => s.name).join(", ")}`);
|
|
13
|
+
console.error(formatError({ message: `No serializer found for lexicon "${args.lexicon}". Available: ${ctx.serializers.map((s) => s.name).join(", ")}` }));
|
|
14
14
|
return 1;
|
|
15
15
|
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
const buildFormat = (args.format || "json") as "json" | "yaml";
|
|
19
19
|
if (buildFormat !== "json" && buildFormat !== "yaml") {
|
|
20
|
-
console.error(`Invalid format for build: ${buildFormat}. Expected 'json' or 'yaml'.`);
|
|
20
|
+
console.error(formatError({ message: `Invalid format for build: ${buildFormat}. Expected 'json' or 'yaml'.` }));
|
|
21
21
|
return 1;
|
|
22
22
|
}
|
|
23
23
|
|
package/src/cli/handlers/lint.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { lintCommand, lintCommandWatch, printLintResult } from "../commands/lint";
|
|
2
|
-
import { formatInfo } from "../format";
|
|
2
|
+
import { formatError, formatInfo } from "../format";
|
|
3
3
|
import type { CommandContext } from "../registry";
|
|
4
4
|
|
|
5
5
|
export async function runLint(ctx: CommandContext): Promise<number> {
|
|
@@ -7,7 +7,7 @@ export async function runLint(ctx: CommandContext): Promise<number> {
|
|
|
7
7
|
|
|
8
8
|
const lintFormat = (args.format || "stylish") as "stylish" | "json" | "sarif";
|
|
9
9
|
if (lintFormat !== "stylish" && lintFormat !== "json" && lintFormat !== "sarif") {
|
|
10
|
-
console.error(`Invalid format for lint: ${lintFormat}. Expected 'stylish', 'json', or 'sarif'.`);
|
|
10
|
+
console.error(formatError({ message: `Invalid format for lint: ${lintFormat}. Expected 'stylish', 'json', or 'sarif'.` }));
|
|
11
11
|
return 1;
|
|
12
12
|
}
|
|
13
13
|
|
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
import { resolve, join } from "node:path";
|
|
2
|
+
import { writeFileSync, unlinkSync, readFileSync } from "node:fs";
|
|
3
|
+
import { mkdirSync, existsSync } from "node:fs";
|
|
4
|
+
import { getRuntime } from "../../runtime-adapter";
|
|
5
|
+
import { discoverSpells } from "../../spell/discovery";
|
|
6
|
+
import { generatePrompt } from "../../spell/prompt";
|
|
7
|
+
import { formatError, formatWarning, formatSuccess, formatBold } from "../format";
|
|
8
|
+
import { loadPlugin } from "../plugins";
|
|
9
|
+
import type { CommandContext } from "../registry";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Find the git root directory.
|
|
13
|
+
*/
|
|
14
|
+
async function findGitRoot(): Promise<string> {
|
|
15
|
+
const rt = getRuntime();
|
|
16
|
+
const result = await rt.spawn(["git", "rev-parse", "--show-toplevel"]);
|
|
17
|
+
if (result.exitCode !== 0) throw new Error("Not in a git repository");
|
|
18
|
+
return result.stdout.trim();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* chant spell add <name>
|
|
23
|
+
*/
|
|
24
|
+
export async function runSpellAdd(ctx: CommandContext): Promise<number> {
|
|
25
|
+
const name = ctx.args.extraPositional;
|
|
26
|
+
if (!name) {
|
|
27
|
+
console.error(formatError({ message: "Name is required: chant spell add <name>" }));
|
|
28
|
+
return 1;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// Validate name format
|
|
32
|
+
if (!/^[a-z0-9]+(-[a-z0-9]+)*$/.test(name) || name.length > 64) {
|
|
33
|
+
console.error(formatError({ message: `Invalid spell name: "${name}" (must be kebab-case, max 64 chars)` }));
|
|
34
|
+
return 1;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const gitRoot = await findGitRoot();
|
|
38
|
+
const spellsDir = join(gitRoot, "spells");
|
|
39
|
+
const filePath = join(spellsDir, `${name}.spell.ts`);
|
|
40
|
+
|
|
41
|
+
if (existsSync(filePath)) {
|
|
42
|
+
console.error(formatError({ message: `Spell "${name}" already exists at ${filePath}` }));
|
|
43
|
+
return 1;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
mkdirSync(spellsDir, { recursive: true });
|
|
47
|
+
|
|
48
|
+
const template = `import { spell, task } from "@intentius/chant";
|
|
49
|
+
|
|
50
|
+
export default spell({
|
|
51
|
+
name: "${name}",
|
|
52
|
+
overview: "",
|
|
53
|
+
tasks: [
|
|
54
|
+
task(""),
|
|
55
|
+
],
|
|
56
|
+
});
|
|
57
|
+
`;
|
|
58
|
+
|
|
59
|
+
writeFileSync(filePath, template);
|
|
60
|
+
console.error(formatSuccess(`Created ${filePath}`));
|
|
61
|
+
return 0;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* chant spell rm <name>
|
|
66
|
+
*/
|
|
67
|
+
export async function runSpellRm(ctx: CommandContext): Promise<number> {
|
|
68
|
+
const name = ctx.args.extraPositional;
|
|
69
|
+
if (!name) {
|
|
70
|
+
console.error(formatError({ message: "Name is required: chant spell rm <name>" }));
|
|
71
|
+
return 1;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const gitRoot = await findGitRoot();
|
|
75
|
+
const filePath = join(gitRoot, "spells", `${name}.spell.ts`);
|
|
76
|
+
|
|
77
|
+
if (!existsSync(filePath)) {
|
|
78
|
+
console.error(formatError({ message: `Spell "${name}" not found at ${filePath}` }));
|
|
79
|
+
return 1;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Check for dependents (unless --force)
|
|
83
|
+
if (!ctx.args.force) {
|
|
84
|
+
const { spells } = await discoverSpells();
|
|
85
|
+
const dependents = [];
|
|
86
|
+
for (const [depName, spell] of spells) {
|
|
87
|
+
if (spell.definition.depends?.includes(name)) {
|
|
88
|
+
dependents.push(depName);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (dependents.length > 0) {
|
|
92
|
+
console.error(formatWarning({
|
|
93
|
+
message: `Spell "${name}" is depended on by: ${dependents.join(", ")}`,
|
|
94
|
+
hint: "Use --force to delete anyway",
|
|
95
|
+
}));
|
|
96
|
+
return 1;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
unlinkSync(filePath);
|
|
101
|
+
console.error(formatSuccess(`Removed ${filePath}`));
|
|
102
|
+
return 0;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* chant spell list
|
|
107
|
+
*/
|
|
108
|
+
export async function runSpellList(ctx: CommandContext): Promise<number> {
|
|
109
|
+
const { spells, errors } = await discoverSpells();
|
|
110
|
+
|
|
111
|
+
for (const err of errors) {
|
|
112
|
+
console.error(formatError({ message: err }));
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (spells.size === 0) {
|
|
116
|
+
console.error(formatWarning({ message: "No spells found" }));
|
|
117
|
+
return 0;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Filter by --ready flag if present (using extraPositional as a hack)
|
|
121
|
+
const readyOnly = ctx.args.format === "ready";
|
|
122
|
+
|
|
123
|
+
console.log(
|
|
124
|
+
"NAME".padEnd(20) +
|
|
125
|
+
"STATUS".padEnd(10) +
|
|
126
|
+
"TASKS".padEnd(10) +
|
|
127
|
+
"LEXICON".padEnd(12) +
|
|
128
|
+
"OVERVIEW"
|
|
129
|
+
);
|
|
130
|
+
|
|
131
|
+
for (const [name, spell] of spells) {
|
|
132
|
+
if (readyOnly && spell.status !== "ready") continue;
|
|
133
|
+
|
|
134
|
+
const def = spell.definition;
|
|
135
|
+
const doneCount = def.tasks.filter((t) => t.done).length;
|
|
136
|
+
const tasksStr = `[${doneCount}/${def.tasks.length}]`;
|
|
137
|
+
const lexicon = def.lexicon ?? "";
|
|
138
|
+
const overview = def.overview.length > 40
|
|
139
|
+
? def.overview.slice(0, 37) + "..."
|
|
140
|
+
: def.overview;
|
|
141
|
+
|
|
142
|
+
console.log(
|
|
143
|
+
name.padEnd(20) +
|
|
144
|
+
spell.status.padEnd(10) +
|
|
145
|
+
tasksStr.padEnd(10) +
|
|
146
|
+
lexicon.padEnd(12) +
|
|
147
|
+
overview
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return 0;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* chant spell show <name>
|
|
156
|
+
*/
|
|
157
|
+
export async function runSpellShow(ctx: CommandContext): Promise<number> {
|
|
158
|
+
const name = ctx.args.extraPositional;
|
|
159
|
+
if (!name) {
|
|
160
|
+
console.error(formatError({ message: "Name is required: chant spell show <name>" }));
|
|
161
|
+
return 1;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const { spells, errors } = await discoverSpells();
|
|
165
|
+
const spell = spells.get(name);
|
|
166
|
+
|
|
167
|
+
if (!spell) {
|
|
168
|
+
// Try to reconstruct from git history
|
|
169
|
+
const rt = getRuntime();
|
|
170
|
+
const result = await rt.spawn([
|
|
171
|
+
"git", "log", "--all", "--format=%H", "--diff-filter=D",
|
|
172
|
+
"--", `spells/${name}.spell.ts`,
|
|
173
|
+
]);
|
|
174
|
+
if (result.exitCode === 0 && result.stdout.trim()) {
|
|
175
|
+
const commit = result.stdout.trim().split("\n")[0];
|
|
176
|
+
const showResult = await rt.spawn([
|
|
177
|
+
"git", "show", `${commit}^:spells/${name}.spell.ts`,
|
|
178
|
+
]);
|
|
179
|
+
if (showResult.exitCode === 0) {
|
|
180
|
+
console.log(`(Reconstructed from git history, commit ${commit.slice(0, 7)})\n`);
|
|
181
|
+
console.log(showResult.stdout);
|
|
182
|
+
return 0;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
console.error(formatError({ message: `Spell "${name}" not found` }));
|
|
187
|
+
return 1;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const def = spell.definition;
|
|
191
|
+
const doneCount = def.tasks.filter((t) => t.done).length;
|
|
192
|
+
|
|
193
|
+
console.log(formatBold(def.name));
|
|
194
|
+
console.log(`Status: ${spell.status} [${doneCount}/${def.tasks.length}]`);
|
|
195
|
+
if (def.lexicon) console.log(`Lexicon: ${def.lexicon}`);
|
|
196
|
+
console.log(`\n${def.overview}\n`);
|
|
197
|
+
|
|
198
|
+
console.log("Tasks:");
|
|
199
|
+
def.tasks.forEach((t, i) => {
|
|
200
|
+
const check = t.done ? "[x]" : "[ ]";
|
|
201
|
+
console.log(` ${i + 1}. ${check} ${t.description}`);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
if (def.depends && def.depends.length > 0) {
|
|
205
|
+
console.log(`\nDepends: ${def.depends.join(", ")}`);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (def.afterAll && def.afterAll.length > 0) {
|
|
209
|
+
console.log(`\nAfter all: ${def.afterAll.join(", ")}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* chant spell cast <name> — generate bootstrap prompt
|
|
217
|
+
*/
|
|
218
|
+
export async function runSpellCast(ctx: CommandContext): Promise<number> {
|
|
219
|
+
const name = ctx.args.extraPositional;
|
|
220
|
+
if (!name) {
|
|
221
|
+
console.error(formatError({ message: "Name is required: chant spell cast <name>" }));
|
|
222
|
+
return 1;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const { spells, errors } = await discoverSpells();
|
|
226
|
+
|
|
227
|
+
for (const err of errors) {
|
|
228
|
+
console.error(formatError({ message: err }));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const spell = spells.get(name);
|
|
232
|
+
if (!spell) {
|
|
233
|
+
console.error(formatError({ message: `Spell "${name}" not found` }));
|
|
234
|
+
return 1;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Warn if blocked or done (unless --force)
|
|
238
|
+
if (spell.status === "blocked" && !ctx.args.force) {
|
|
239
|
+
console.error(formatWarning({
|
|
240
|
+
message: `Spell "${name}" is blocked by incomplete dependencies`,
|
|
241
|
+
hint: "Use --force to proceed anyway",
|
|
242
|
+
}));
|
|
243
|
+
return 1;
|
|
244
|
+
}
|
|
245
|
+
if (spell.status === "done" && !ctx.args.force) {
|
|
246
|
+
console.error(formatWarning({
|
|
247
|
+
message: `Spell "${name}" is already done`,
|
|
248
|
+
hint: "Use --force to proceed anyway",
|
|
249
|
+
}));
|
|
250
|
+
return 1;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const gitRoot = await findGitRoot();
|
|
254
|
+
|
|
255
|
+
// Load the spell's lexicon plugin directly (not all project lexicons)
|
|
256
|
+
const plugins: import("../../lexicon").LexiconPlugin[] = [];
|
|
257
|
+
if (spell.definition.lexicon) {
|
|
258
|
+
try {
|
|
259
|
+
const plugin = await loadPlugin(spell.definition.lexicon);
|
|
260
|
+
if (plugin.init) await plugin.init();
|
|
261
|
+
plugins.push(plugin);
|
|
262
|
+
} catch {
|
|
263
|
+
console.error(formatWarning({
|
|
264
|
+
message: `Lexicon "${spell.definition.lexicon}" could not be loaded — skills will not be inlined`,
|
|
265
|
+
hint: `Install @intentius/chant-lexicon-${spell.definition.lexicon}`,
|
|
266
|
+
}));
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
const prompt = await generatePrompt(spell.definition, {
|
|
271
|
+
gitRoot,
|
|
272
|
+
plugins: plugins.length > 0 ? plugins : undefined,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
console.log(prompt);
|
|
276
|
+
return 0;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* chant spell done <name> <task-number>
|
|
281
|
+
*
|
|
282
|
+
* Rewrites task() call in the source file to mark it as done.
|
|
283
|
+
*/
|
|
284
|
+
export async function runSpellDone(ctx: CommandContext): Promise<number> {
|
|
285
|
+
const name = ctx.args.extraPositional;
|
|
286
|
+
const taskNumStr = ctx.args.extraPositional2;
|
|
287
|
+
|
|
288
|
+
if (!name || !taskNumStr) {
|
|
289
|
+
console.error(formatError({ message: "Usage: chant spell done <name> <task-number>" }));
|
|
290
|
+
return 1;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const taskNum = parseInt(taskNumStr, 10);
|
|
294
|
+
if (isNaN(taskNum) || taskNum < 1) {
|
|
295
|
+
console.error(formatError({ message: `Invalid task number: ${taskNumStr}` }));
|
|
296
|
+
return 1;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const { spells } = await discoverSpells();
|
|
300
|
+
const spell = spells.get(name);
|
|
301
|
+
if (!spell) {
|
|
302
|
+
console.error(formatError({ message: `Spell "${name}" not found` }));
|
|
303
|
+
return 1;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (taskNum > spell.definition.tasks.length) {
|
|
307
|
+
console.error(formatError({
|
|
308
|
+
message: `Task ${taskNum} does not exist (spell has ${spell.definition.tasks.length} tasks)`,
|
|
309
|
+
}));
|
|
310
|
+
return 1;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
const task = spell.definition.tasks[taskNum - 1];
|
|
314
|
+
if (task.done) {
|
|
315
|
+
console.error(formatWarning({ message: `Task ${taskNum} is already done` }));
|
|
316
|
+
return 0;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// Rewrite the source file
|
|
320
|
+
const content = readFileSync(spell.filePath, "utf-8");
|
|
321
|
+
const rewritten = markTaskDone(content, taskNum);
|
|
322
|
+
|
|
323
|
+
if (rewritten === content) {
|
|
324
|
+
console.error(formatError({ message: `Could not find task ${taskNum} in source file` }));
|
|
325
|
+
return 1;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
writeFileSync(spell.filePath, rewritten);
|
|
329
|
+
console.error(formatSuccess(`Task ${taskNum} marked done: "${task.description}"`));
|
|
330
|
+
return 0;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Regex-based rewrite: find the Nth task() call and add { done: true }.
|
|
335
|
+
*/
|
|
336
|
+
function markTaskDone(source: string, taskNum: number): string {
|
|
337
|
+
let count = 0;
|
|
338
|
+
// Match task("...") or task("...", { done: false })
|
|
339
|
+
return source.replace(
|
|
340
|
+
/task\(("[^"]*"|'[^']*'|`[^`]*`)((?:\s*,\s*\{[^}]*\})?)\)/g,
|
|
341
|
+
(match, desc, opts) => {
|
|
342
|
+
count++;
|
|
343
|
+
if (count !== taskNum) return match;
|
|
344
|
+
|
|
345
|
+
// Already has opts — replace done: false with done: true or add done: true
|
|
346
|
+
if (opts && opts.includes("done:")) {
|
|
347
|
+
return match.replace(/done:\s*false/, "done: true");
|
|
348
|
+
}
|
|
349
|
+
return `task(${desc}, { done: true })`;
|
|
350
|
+
},
|
|
351
|
+
);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* chant graph — show dependency graph
|
|
356
|
+
*/
|
|
357
|
+
export async function runGraph(ctx: CommandContext): Promise<number> {
|
|
358
|
+
const { spells, errors } = await discoverSpells();
|
|
359
|
+
|
|
360
|
+
for (const err of errors) {
|
|
361
|
+
console.error(formatError({ message: err }));
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
if (spells.size === 0) {
|
|
365
|
+
console.error(formatWarning({ message: "No spells found" }));
|
|
366
|
+
return 0;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
let hasEdges = false;
|
|
370
|
+
for (const [name, spell] of spells) {
|
|
371
|
+
const deps = spell.definition.depends;
|
|
372
|
+
if (deps && deps.length > 0) {
|
|
373
|
+
for (const dep of deps) {
|
|
374
|
+
console.log(`${dep} → ${name}`);
|
|
375
|
+
hasEdges = true;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
if (!hasEdges) {
|
|
381
|
+
console.log("No dependencies");
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
return 0;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Fallback for unknown spell subcommands.
|
|
389
|
+
*/
|
|
390
|
+
export async function runSpellUnknown(ctx: CommandContext): Promise<number> {
|
|
391
|
+
console.error(formatError({
|
|
392
|
+
message: `Unknown spell subcommand: ${ctx.args.extraPositional ?? ctx.args.path}`,
|
|
393
|
+
hint: "Available: chant spell add, chant spell rm, chant spell list, chant spell show, chant spell cast, chant spell done",
|
|
394
|
+
}));
|
|
395
|
+
return 1;
|
|
396
|
+
}
|