@di-framework/cli-plugin-ai 0.0.1 → 6.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.
@@ -0,0 +1,145 @@
1
+ import { createRequire } from 'node:module';
2
+ import { resolve } from 'node:path';
3
+ import { pathToFileURL } from 'node:url';
4
+ import type {
5
+ ResolveSkillSourcesOptions,
6
+ SkillCatalogDiagnostic,
7
+ validateSkillCatalog,
8
+ } from '@di-framework/ai-utils';
9
+ import { CommandFailure, type CommandResult, type JsonValue } from '@di-framework/cli-extension';
10
+
11
+ export type SkillsValidateOperations = {
12
+ readonly validateSkillCatalog: typeof validateSkillCatalog;
13
+ };
14
+
15
+ export interface SkillsValidateCliOptions {
16
+ readonly workspace: string;
17
+ readonly userDirectory?: string;
18
+ readonly directories?: readonly string[];
19
+ readonly packages?: readonly string[];
20
+ readonly sourceMode?: 'merge' | 'replace';
21
+ }
22
+
23
+ export interface SkillsValidateCommandResult {
24
+ readonly valid: boolean;
25
+ readonly skillCount: number;
26
+ readonly diagnostics: readonly SkillCatalogDiagnostic[];
27
+ }
28
+
29
+ export function parseSkillsValidateArgs(
30
+ args: readonly string[],
31
+ cwd = process.cwd(),
32
+ ): SkillsValidateCliOptions {
33
+ let workspace = cwd;
34
+ let workspaceConfigured = false;
35
+ let userDirectory: string | undefined;
36
+ let sourceMode: 'merge' | 'replace' | undefined;
37
+ const directories: string[] = [];
38
+ const packages: string[] = [];
39
+
40
+ for (let position = 0; position < args.length; position++) {
41
+ const token = args[position] ?? '';
42
+ switch (token) {
43
+ case '--workspace':
44
+ if (workspaceConfigured) duplicateOption(token);
45
+ workspace = readOptionValue(args, ++position, token);
46
+ workspaceConfigured = true;
47
+ break;
48
+ case '--user-directory':
49
+ if (userDirectory != null) duplicateOption(token);
50
+ userDirectory = readOptionValue(args, ++position, token);
51
+ break;
52
+ case '--skills-dir':
53
+ directories.push(readOptionValue(args, ++position, token));
54
+ break;
55
+ case '--skills-package':
56
+ packages.push(readOptionValue(args, ++position, token));
57
+ break;
58
+ case '--source-mode': {
59
+ if (sourceMode != null) duplicateOption(token);
60
+ const value = readOptionValue(args, ++position, token);
61
+ if (value !== 'merge' && value !== 'replace') {
62
+ invalidUsage(`Invalid value for --source-mode: ${value}`, token, { value });
63
+ }
64
+ sourceMode = value;
65
+ break;
66
+ }
67
+ default:
68
+ invalidUsage(`Unknown option or argument: ${token}`, token);
69
+ }
70
+ }
71
+
72
+ return {
73
+ workspace,
74
+ userDirectory,
75
+ directories: directories.length ? directories : undefined,
76
+ packages: packages.length ? packages : undefined,
77
+ sourceMode,
78
+ };
79
+ }
80
+
81
+ export async function runSkillsValidate(
82
+ args: readonly string[],
83
+ operations?: SkillsValidateOperations,
84
+ cwd = process.cwd(),
85
+ ): Promise<CommandResult> {
86
+ const options = parseSkillsValidateArgs(args, cwd);
87
+ const api = operations ?? (await loadSkillsValidateOperations(cwd));
88
+ const packageOptions: ResolveSkillSourcesOptions = options;
89
+ const result = api.validateSkillCatalog(packageOptions);
90
+ const presentation: SkillsValidateCommandResult = {
91
+ valid: result.valid,
92
+ skillCount: result.skills.length,
93
+ diagnostics: result.diagnostics,
94
+ };
95
+ return {
96
+ data: presentation as unknown as JsonValue,
97
+ text: formatSkillsValidation(presentation),
98
+ exitCode: result.valid ? 0 : 1,
99
+ };
100
+ }
101
+
102
+ export function formatSkillsValidation(result: SkillsValidateCommandResult): string {
103
+ const state = result.valid ? 'valid' : 'invalid';
104
+ const summary = `Skill catalog is ${state}: ${result.skillCount} skill(s), ${result.diagnostics.length} diagnostic(s)`;
105
+ const diagnostics = result.diagnostics.map((diagnostic) => {
106
+ const related = diagnostic.relatedPath ? ` (related: ${diagnostic.relatedPath})` : '';
107
+ return `[${diagnostic.severity.toUpperCase()} ${diagnostic.code}] ${diagnostic.path}: ${diagnostic.message}${related}`;
108
+ });
109
+ return [summary, ...diagnostics].join('\n');
110
+ }
111
+
112
+ async function loadSkillsValidateOperations(cwd: string): Promise<SkillsValidateOperations> {
113
+ try {
114
+ const projectRequire = createRequire(resolve(cwd, 'package.json'));
115
+ const modulePath = projectRequire.resolve('@di-framework/ai-utils');
116
+ return import(pathToFileURL(modulePath).href);
117
+ } catch (cause) {
118
+ throw new CommandFailure(
119
+ 'SKILLS_PACKAGE_UNAVAILABLE',
120
+ 'Unable to load @di-framework/ai-utils from the current project',
121
+ 3,
122
+ { cause: cause instanceof Error ? cause.message : String(cause) },
123
+ );
124
+ }
125
+ }
126
+
127
+ function readOptionValue(args: readonly string[], position: number, option: string): string {
128
+ const value = args[position];
129
+ if (value == null || value.startsWith('--')) {
130
+ invalidUsage(`Missing value for ${option}`, option);
131
+ }
132
+ return value;
133
+ }
134
+
135
+ function duplicateOption(option: string): never {
136
+ invalidUsage(`Option may be provided only once: ${option}`, option);
137
+ }
138
+
139
+ function invalidUsage(
140
+ message: string,
141
+ token: string,
142
+ details: Record<string, JsonValue> = {},
143
+ ): never {
144
+ throw new CommandFailure('INVALID_USAGE', message, 2, { token, ...details });
145
+ }