@di-framework/cli-plugin-ai 0.0.1 → 6.0.3
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/README.md +21 -0
- package/package.json +36 -9
- package/src/agent/audit.ts +169 -0
- package/src/agent/init.ts +168 -0
- package/src/agent/inspect.ts +252 -0
- package/src/agent/migrate.ts +215 -0
- package/src/command.ts +164 -0
- package/src/index.ts +23 -2
- package/src/skills/index.ts +276 -0
- package/src/skills/validate.ts +145 -0
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import type {
|
|
5
|
+
AgentConfigurationAuditFinding,
|
|
6
|
+
AgentConfigurationMigrationAction,
|
|
7
|
+
AgentConfigurationMigrationActionResult,
|
|
8
|
+
AgentConfigurationMigrationExecutionResult,
|
|
9
|
+
AgentConfigurationMigrationPlan,
|
|
10
|
+
AuditAgentConfigurationOptions,
|
|
11
|
+
auditAgentConfiguration,
|
|
12
|
+
executeAgentConfigurationMigration,
|
|
13
|
+
planAgentConfigurationMigration,
|
|
14
|
+
} from '@di-framework/ai-utils';
|
|
15
|
+
import { CommandFailure, type CommandResult, type JsonValue } from '@di-framework/cli-extension';
|
|
16
|
+
|
|
17
|
+
export type AgentMigrateOperations = {
|
|
18
|
+
readonly auditAgentConfiguration: typeof auditAgentConfiguration;
|
|
19
|
+
readonly planAgentConfigurationMigration: typeof planAgentConfigurationMigration;
|
|
20
|
+
readonly executeAgentConfigurationMigration: typeof executeAgentConfigurationMigration;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface AgentMigrateCliOptions {
|
|
24
|
+
readonly mode: 'plan' | 'apply';
|
|
25
|
+
readonly audit: AuditAgentConfigurationOptions;
|
|
26
|
+
readonly opportunityPaths?: readonly string[];
|
|
27
|
+
readonly replaceExisting: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface AgentMigrateCommandData {
|
|
31
|
+
readonly mode: 'plan' | 'apply';
|
|
32
|
+
readonly audit: {
|
|
33
|
+
readonly valid: boolean;
|
|
34
|
+
readonly findings: readonly AgentConfigurationAuditFinding[];
|
|
35
|
+
};
|
|
36
|
+
readonly plan: AgentConfigurationMigrationPlan;
|
|
37
|
+
readonly execution?: AgentConfigurationMigrationExecutionResult;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const REPEATABLE_OPTIONS = new Set([
|
|
41
|
+
'--skills-dir',
|
|
42
|
+
'--skills-package',
|
|
43
|
+
'--instructions-fallback',
|
|
44
|
+
'--source',
|
|
45
|
+
]);
|
|
46
|
+
const VALUE_OPTIONS = new Set([
|
|
47
|
+
'--workspace',
|
|
48
|
+
'--working-directory',
|
|
49
|
+
'--user-directory',
|
|
50
|
+
'--source-mode',
|
|
51
|
+
'--max-instruction-bytes',
|
|
52
|
+
]);
|
|
53
|
+
const FLAG_OPTIONS = new Set(['--plan', '--apply', '--replace-existing']);
|
|
54
|
+
|
|
55
|
+
export async function runAgentMigrate(
|
|
56
|
+
args: readonly string[],
|
|
57
|
+
operations?: AgentMigrateOperations,
|
|
58
|
+
cwd = process.cwd(),
|
|
59
|
+
): Promise<CommandResult> {
|
|
60
|
+
const options = parseAgentMigrateArgs(args, cwd);
|
|
61
|
+
const api = operations ?? (await loadAgentMigrateOperations(cwd));
|
|
62
|
+
const report = api.auditAgentConfiguration(options.audit);
|
|
63
|
+
const plan = api.planAgentConfigurationMigration(report, {
|
|
64
|
+
opportunityPaths: options.opportunityPaths,
|
|
65
|
+
replaceExisting: options.replaceExisting,
|
|
66
|
+
});
|
|
67
|
+
const execution =
|
|
68
|
+
options.mode === 'apply'
|
|
69
|
+
? api.executeAgentConfigurationMigration(plan, { dryRun: false })
|
|
70
|
+
: undefined;
|
|
71
|
+
const sharedData = {
|
|
72
|
+
mode: options.mode,
|
|
73
|
+
audit: { valid: report.valid, findings: report.findings },
|
|
74
|
+
plan,
|
|
75
|
+
};
|
|
76
|
+
const data: AgentMigrateCommandData =
|
|
77
|
+
execution == null ? sharedData : { ...sharedData, execution };
|
|
78
|
+
const successful = report.valid && plan.valid && (execution?.success ?? true);
|
|
79
|
+
return {
|
|
80
|
+
data: data as unknown as JsonValue,
|
|
81
|
+
text: formatAgentMigration(data),
|
|
82
|
+
exitCode: successful ? 0 : 1,
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function parseAgentMigrateArgs(
|
|
87
|
+
args: readonly string[],
|
|
88
|
+
cwd = process.cwd(),
|
|
89
|
+
): AgentMigrateCliOptions {
|
|
90
|
+
const values = new Map<string, string>();
|
|
91
|
+
const repeated = new Map<string, string[]>();
|
|
92
|
+
const flags = new Set<string>();
|
|
93
|
+
for (let position = 0; position < args.length; position++) {
|
|
94
|
+
const option = args[position] ?? '';
|
|
95
|
+
if (FLAG_OPTIONS.has(option)) {
|
|
96
|
+
if (flags.has(option)) invalidUsage(`Option may be provided only once: ${option}`, option);
|
|
97
|
+
flags.add(option);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
if (!VALUE_OPTIONS.has(option) && !REPEATABLE_OPTIONS.has(option)) {
|
|
101
|
+
invalidUsage(`Unknown option or argument: ${option}`, option);
|
|
102
|
+
}
|
|
103
|
+
const value = args[++position];
|
|
104
|
+
if (value == null || value.startsWith('--')) {
|
|
105
|
+
invalidUsage(`Missing value for ${option}`, option);
|
|
106
|
+
}
|
|
107
|
+
if (REPEATABLE_OPTIONS.has(option)) {
|
|
108
|
+
repeated.set(option, [...(repeated.get(option) ?? []), value]);
|
|
109
|
+
} else {
|
|
110
|
+
if (values.has(option)) invalidUsage(`Option may be provided only once: ${option}`, option);
|
|
111
|
+
values.set(option, value);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (flags.has('--plan') && flags.has('--apply')) {
|
|
115
|
+
invalidUsage('--plan and --apply are mutually exclusive', '--apply');
|
|
116
|
+
}
|
|
117
|
+
const sourceMode = values.get('--source-mode');
|
|
118
|
+
if (sourceMode != null && sourceMode !== 'merge' && sourceMode !== 'replace') {
|
|
119
|
+
invalidUsage('--source-mode must be merge or replace', '--source-mode');
|
|
120
|
+
}
|
|
121
|
+
const maxBytesValue = values.get('--max-instruction-bytes');
|
|
122
|
+
const maxBytes = maxBytesValue == null ? undefined : Number(maxBytesValue);
|
|
123
|
+
if (maxBytes != null && (!Number.isSafeInteger(maxBytes) || maxBytes < 0)) {
|
|
124
|
+
invalidUsage(
|
|
125
|
+
'--max-instruction-bytes must be a non-negative safe integer',
|
|
126
|
+
'--max-instruction-bytes',
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const workspace = resolve(cwd, values.get('--workspace') ?? '.');
|
|
131
|
+
return {
|
|
132
|
+
mode: flags.has('--apply') ? 'apply' : 'plan',
|
|
133
|
+
audit: {
|
|
134
|
+
workspace,
|
|
135
|
+
workingDirectory: resolve(workspace, values.get('--working-directory') ?? '.'),
|
|
136
|
+
userDirectory: optionalResolved(workspace, values.get('--user-directory')),
|
|
137
|
+
directories: repeatedValues(repeated, '--skills-dir'),
|
|
138
|
+
packages: repeatedValues(repeated, '--skills-package'),
|
|
139
|
+
sourceMode: sourceMode as 'merge' | 'replace' | undefined,
|
|
140
|
+
fallbackFilenames: repeatedValues(repeated, '--instructions-fallback'),
|
|
141
|
+
maxBytes,
|
|
142
|
+
},
|
|
143
|
+
opportunityPaths: repeatedValues(repeated, '--source'),
|
|
144
|
+
replaceExisting: flags.has('--replace-existing'),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function formatAgentMigration(result: AgentMigrateCommandData): string {
|
|
149
|
+
const lines = [
|
|
150
|
+
`Agent migration ${result.mode} for ${result.plan.workspace}`,
|
|
151
|
+
`Audit: ${result.audit.valid ? 'valid' : 'invalid'} (${result.audit.findings.length} findings)`,
|
|
152
|
+
`Plan: ${result.plan.valid ? 'valid' : 'invalid'} (${result.plan.actions.length} actions)`,
|
|
153
|
+
'Planned actions:',
|
|
154
|
+
...listOrNone(result.plan.actions.map(formatPlanAction)),
|
|
155
|
+
];
|
|
156
|
+
if (result.execution == null) {
|
|
157
|
+
lines.push('No files changed (plan mode).');
|
|
158
|
+
} else {
|
|
159
|
+
lines.push(
|
|
160
|
+
`Execution: ${result.execution.applied.length} applied, ${result.execution.skipped.length} skipped, ${result.execution.failed.length} failed`,
|
|
161
|
+
'Applied actions:',
|
|
162
|
+
...listOrNone(result.execution.applied.map(formatActionResult)),
|
|
163
|
+
'Skipped actions:',
|
|
164
|
+
...listOrNone(result.execution.skipped.map(formatActionResult)),
|
|
165
|
+
'Failed actions:',
|
|
166
|
+
...listOrNone(result.execution.failed.map(formatActionResult)),
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
return lines.join('\n');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function formatPlanAction(action: AgentConfigurationMigrationAction): string {
|
|
173
|
+
const source = action.source?.kind === 'file' ? ` <- ${action.source.path}` : '';
|
|
174
|
+
return ` ${action.id} [${action.status}:${action.code}] ${action.operation} ${action.relativeTargetPath}${source}`;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function formatActionResult(result: AgentConfigurationMigrationActionResult): string {
|
|
178
|
+
const backup = result.backupPath == null ? '' : ` (backup: ${result.backupPath})`;
|
|
179
|
+
return ` ${result.actionId} [${result.status}:${result.code}] ${result.targetPath}${backup}`;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function listOrNone(lines: readonly string[]): readonly string[] {
|
|
183
|
+
return lines.length === 0 ? [' (none)'] : lines;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function optionalResolved(base: string, value: string | undefined): string | undefined {
|
|
187
|
+
return value == null ? undefined : resolve(base, value);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function repeatedValues(
|
|
191
|
+
values: ReadonlyMap<string, readonly string[]>,
|
|
192
|
+
option: string,
|
|
193
|
+
): readonly string[] | undefined {
|
|
194
|
+
const entries = values.get(option);
|
|
195
|
+
return entries?.length ? entries : undefined;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async function loadAgentMigrateOperations(cwd: string): Promise<AgentMigrateOperations> {
|
|
199
|
+
try {
|
|
200
|
+
const projectRequire = createRequire(resolve(cwd, 'package.json'));
|
|
201
|
+
const modulePath = projectRequire.resolve('@di-framework/ai-utils');
|
|
202
|
+
return import(pathToFileURL(modulePath).href);
|
|
203
|
+
} catch (cause) {
|
|
204
|
+
throw new CommandFailure(
|
|
205
|
+
'AGENT_MIGRATE_PACKAGE_UNAVAILABLE',
|
|
206
|
+
'Unable to load @di-framework/ai-utils from the current project',
|
|
207
|
+
3,
|
|
208
|
+
{ cause: cause instanceof Error ? cause.message : String(cause) },
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function invalidUsage(message: string, token: string): never {
|
|
214
|
+
throw new CommandFailure('INVALID_USAGE', message, 2, { token });
|
|
215
|
+
}
|
package/src/command.ts
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { CommandNode } from '@di-framework/cli-extension';
|
|
2
|
+
import { runAgentAudit } from './agent/audit.ts';
|
|
3
|
+
import { runAgentInit } from './agent/init.ts';
|
|
4
|
+
import { runAgentInspect } from './agent/inspect.ts';
|
|
5
|
+
import { runAgentMigrate } from './agent/migrate.ts';
|
|
6
|
+
import {
|
|
7
|
+
runSkillsIndexBuild,
|
|
8
|
+
runSkillsIndexInspect,
|
|
9
|
+
runSkillsIndexMigrate,
|
|
10
|
+
runSkillsIndexQuery,
|
|
11
|
+
runSkillsIndexValidate,
|
|
12
|
+
} from './skills/index.ts';
|
|
13
|
+
import { runSkillsValidate } from './skills/validate.ts';
|
|
14
|
+
|
|
15
|
+
/** `di-framework ai` command tree. Agent and skills commands moved here from the CLI host. */
|
|
16
|
+
export function createAiCommand(): CommandNode {
|
|
17
|
+
return {
|
|
18
|
+
description: 'Inspect and manage agent configuration and Agent Skills',
|
|
19
|
+
children: {
|
|
20
|
+
agent: {
|
|
21
|
+
description: 'Inspect and manage agent configuration',
|
|
22
|
+
children: {
|
|
23
|
+
audit: {
|
|
24
|
+
description: 'Audit agent configuration without changing files',
|
|
25
|
+
usage: 'di-framework ai agent audit [options]',
|
|
26
|
+
options: [
|
|
27
|
+
'--workspace <path> Workspace boundary (default: current directory)',
|
|
28
|
+
'--working-directory <path> Instruction discovery location',
|
|
29
|
+
'--user-directory <path> User-level neutral source root',
|
|
30
|
+
'--skills-dir <path> Explicit skill root (repeatable)',
|
|
31
|
+
'--skills-package <name> Package-provided skill root (repeatable)',
|
|
32
|
+
'--source-mode merge|replace Merge with or replace neutral skill roots',
|
|
33
|
+
'--instructions-fallback <name> Instruction fallback filename (repeatable)',
|
|
34
|
+
'--max-instruction-bytes <count> Combined instruction byte limit',
|
|
35
|
+
'--allowed-directory <path> Allowed-directory intersection (repeatable)',
|
|
36
|
+
],
|
|
37
|
+
run: ({ args }) => runAgentAudit(args),
|
|
38
|
+
},
|
|
39
|
+
init: {
|
|
40
|
+
description: 'Plan or create neutral agent configuration assets',
|
|
41
|
+
usage: 'di-framework ai agent init [options]',
|
|
42
|
+
options: [
|
|
43
|
+
'--workspace <path> Workspace boundary (default: current directory)',
|
|
44
|
+
'--asset <path> Neutral asset to initialize (repeatable; defaults to all)',
|
|
45
|
+
'--dry-run Plan without writing (default)',
|
|
46
|
+
'--apply Apply the exact generated plan',
|
|
47
|
+
],
|
|
48
|
+
run: ({ args }) => runAgentInit(args),
|
|
49
|
+
},
|
|
50
|
+
inspect: {
|
|
51
|
+
description: 'Inspect resolved agent configuration without changing files',
|
|
52
|
+
usage: 'di-framework ai agent inspect [options]',
|
|
53
|
+
options: [
|
|
54
|
+
'--workspace <path> Workspace boundary (default: current directory)',
|
|
55
|
+
'--working-directory <path> Instruction discovery location',
|
|
56
|
+
'--user-directory <path> User-level neutral source root',
|
|
57
|
+
'--skills-dir <path> Explicit skill root (repeatable)',
|
|
58
|
+
'--skills-package <name> Package-provided skill root (repeatable)',
|
|
59
|
+
'--source-mode merge|replace Merge with or replace neutral skill roots',
|
|
60
|
+
'--instructions-fallback <name> Instruction fallback filename (repeatable)',
|
|
61
|
+
'--max-instruction-bytes <count> Combined instruction byte limit',
|
|
62
|
+
],
|
|
63
|
+
run: ({ args }) => runAgentInspect(args),
|
|
64
|
+
},
|
|
65
|
+
migrate: {
|
|
66
|
+
description: 'Plan or apply neutral agent-configuration migrations',
|
|
67
|
+
usage: 'di-framework ai agent migrate [--plan | --apply] [options]',
|
|
68
|
+
options: [
|
|
69
|
+
'--plan Display a migration plan without changing files (default)',
|
|
70
|
+
'--apply Apply exactly the generated migration plan',
|
|
71
|
+
'--workspace <path> Workspace boundary (default: current directory)',
|
|
72
|
+
'--working-directory <path> Instruction audit location',
|
|
73
|
+
'--user-directory <path> User-level neutral source root',
|
|
74
|
+
'--skills-dir <path> Explicit skill root to audit (repeatable)',
|
|
75
|
+
'--skills-package <name> Package-provided skill root (repeatable)',
|
|
76
|
+
'--source-mode merge|replace Merge with or replace neutral skill roots',
|
|
77
|
+
'--instructions-fallback <name> Instruction fallback filename (repeatable)',
|
|
78
|
+
'--max-instruction-bytes <count> Combined instruction byte limit',
|
|
79
|
+
'--source <path> Select an audited migration source (repeatable)',
|
|
80
|
+
'--replace-existing Plan explicit recoverable file replacement',
|
|
81
|
+
],
|
|
82
|
+
run: ({ args }) => runAgentMigrate(args),
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
skills: {
|
|
87
|
+
description: 'Agent Skills operations',
|
|
88
|
+
children: {
|
|
89
|
+
index: {
|
|
90
|
+
description: 'Semantic skills-index operations',
|
|
91
|
+
children: {
|
|
92
|
+
build: {
|
|
93
|
+
description: 'Build a skills index from explicit skill sources',
|
|
94
|
+
usage: 'di-framework ai skills index build [options]',
|
|
95
|
+
options: [
|
|
96
|
+
'--skills-dir <path> SKILL.md tree (repeatable)',
|
|
97
|
+
'--skill-file <path> Individual SKILL.md (repeatable)',
|
|
98
|
+
'--output <path> Index output file',
|
|
99
|
+
'--threshold <count> Minimum catalog size for embeddings',
|
|
100
|
+
'--limit <count> Retrieval candidate limit',
|
|
101
|
+
'--batch-size <count> Embedding batch size',
|
|
102
|
+
'--chunk-tokens <count> Tokens per source chunk',
|
|
103
|
+
'--chunk-overlap <count> Overlap between chunks',
|
|
104
|
+
'--force Rebuild an unchanged index',
|
|
105
|
+
],
|
|
106
|
+
run: ({ args }) => runSkillsIndexBuild(args),
|
|
107
|
+
},
|
|
108
|
+
inspect: {
|
|
109
|
+
description: 'Inspect safe skills-index metadata',
|
|
110
|
+
usage: 'di-framework ai skills index inspect [--input <path>]',
|
|
111
|
+
options: ['--input <path> Index file to inspect'],
|
|
112
|
+
run: ({ args }) => runSkillsIndexInspect(args),
|
|
113
|
+
},
|
|
114
|
+
validate: {
|
|
115
|
+
description: 'Validate index integrity and optional source drift',
|
|
116
|
+
usage: 'di-framework ai skills index validate [options]',
|
|
117
|
+
options: [
|
|
118
|
+
'--input <path> Index file to validate',
|
|
119
|
+
'--skills-dir <path> SKILL.md tree to compare (repeatable)',
|
|
120
|
+
'--skill-file <path> SKILL.md file to compare (repeatable)',
|
|
121
|
+
'--allow-extra-skills Allow indexed skills absent from sources',
|
|
122
|
+
],
|
|
123
|
+
run: ({ args }) => runSkillsIndexValidate(args),
|
|
124
|
+
},
|
|
125
|
+
query: {
|
|
126
|
+
description: 'Query an existing skills index',
|
|
127
|
+
usage: 'di-framework ai skills index query --query <text> [options]',
|
|
128
|
+
options: [
|
|
129
|
+
'--input <path> Index file to query',
|
|
130
|
+
'--query <text> Search query (required)',
|
|
131
|
+
'--limit <count> Maximum matches',
|
|
132
|
+
'--min-score <number> Minimum match score',
|
|
133
|
+
'--abstention-threshold <number> Minimum selection confidence',
|
|
134
|
+
],
|
|
135
|
+
run: ({ args }) => runSkillsIndexQuery(args),
|
|
136
|
+
},
|
|
137
|
+
migrate: {
|
|
138
|
+
description: 'Rewrite a skills index in the current format',
|
|
139
|
+
usage: 'di-framework ai skills index migrate [options]',
|
|
140
|
+
options: [
|
|
141
|
+
'--input <path> Source index file',
|
|
142
|
+
'--output <path> Migrated index output file',
|
|
143
|
+
],
|
|
144
|
+
run: ({ args }) => runSkillsIndexMigrate(args),
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
validate: {
|
|
149
|
+
description: 'Validate discovered Agent Skills catalogs',
|
|
150
|
+
usage: 'di-framework ai skills validate [options]',
|
|
151
|
+
options: [
|
|
152
|
+
'--workspace <path> Workspace root (default: current directory)',
|
|
153
|
+
'--user-directory <path> User root for neutral default discovery',
|
|
154
|
+
'--skills-dir <path> Explicit SKILL.md tree (repeatable)',
|
|
155
|
+
'--skills-package <name-or-path> Package skill source (repeatable)',
|
|
156
|
+
'--source-mode <merge|replace> Merge with or replace neutral defaults',
|
|
157
|
+
],
|
|
158
|
+
run: ({ args }) => runSkillsValidate(args),
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
};
|
|
164
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import { defineExtension } from '@di-framework/cli-extension';
|
|
2
|
+
import { createAiCommand } from './command.ts';
|
|
3
|
+
|
|
4
|
+
export { parseAgentAuditArgs, runAgentAudit } from './agent/audit.ts';
|
|
5
|
+
export { runAgentInit } from './agent/init.ts';
|
|
6
|
+
export { runAgentInspect } from './agent/inspect.ts';
|
|
7
|
+
export { parseAgentMigrateArgs, runAgentMigrate } from './agent/migrate.ts';
|
|
8
|
+
export { createAiCommand } from './command.ts';
|
|
9
|
+
export {
|
|
10
|
+
runSkillsIndexBuild,
|
|
11
|
+
runSkillsIndexInspect,
|
|
12
|
+
runSkillsIndexMigrate,
|
|
13
|
+
runSkillsIndexQuery,
|
|
14
|
+
runSkillsIndexValidate,
|
|
15
|
+
} from './skills/index.ts';
|
|
16
|
+
export { parseSkillsValidateArgs, runSkillsValidate } from './skills/validate.ts';
|
|
17
|
+
|
|
18
|
+
export default defineExtension({
|
|
19
|
+
schemaVersion: 1,
|
|
20
|
+
name: 'ai',
|
|
21
|
+
description: 'Inspect and manage agent configuration and Agent Skills',
|
|
22
|
+
command: createAiCommand(),
|
|
23
|
+
});
|
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { pathToFileURL } from 'node:url';
|
|
4
|
+
import type {
|
|
5
|
+
buildSkillsIndex,
|
|
6
|
+
inspectSkillsIndex,
|
|
7
|
+
migrateSkillsIndex,
|
|
8
|
+
querySkillsIndex,
|
|
9
|
+
SkillsIndexOperationError,
|
|
10
|
+
validateSkillsIndex,
|
|
11
|
+
} from '@di-framework/ai-utils';
|
|
12
|
+
import { CommandFailure, type CommandResult, type JsonValue } from '@di-framework/cli-extension';
|
|
13
|
+
|
|
14
|
+
export type SkillsIndexCommand = 'build' | 'inspect' | 'validate' | 'query' | 'migrate';
|
|
15
|
+
|
|
16
|
+
export type SkillsIndexOperations = {
|
|
17
|
+
readonly buildSkillsIndex: typeof buildSkillsIndex;
|
|
18
|
+
readonly inspectSkillsIndex: typeof inspectSkillsIndex;
|
|
19
|
+
readonly validateSkillsIndex: typeof validateSkillsIndex;
|
|
20
|
+
readonly querySkillsIndex: typeof querySkillsIndex;
|
|
21
|
+
readonly migrateSkillsIndex: typeof migrateSkillsIndex;
|
|
22
|
+
readonly SkillsIndexOperationError: typeof SkillsIndexOperationError;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
type OptionKind = 'value' | 'repeatable' | 'boolean';
|
|
26
|
+
|
|
27
|
+
const OPTION_SPECS: Record<SkillsIndexCommand, Readonly<Record<string, OptionKind>>> = {
|
|
28
|
+
build: {
|
|
29
|
+
'--skills-dir': 'repeatable',
|
|
30
|
+
'--skill-file': 'repeatable',
|
|
31
|
+
'--output': 'value',
|
|
32
|
+
'--threshold': 'value',
|
|
33
|
+
'--limit': 'value',
|
|
34
|
+
'--batch-size': 'value',
|
|
35
|
+
'--chunk-tokens': 'value',
|
|
36
|
+
'--chunk-overlap': 'value',
|
|
37
|
+
'--force': 'boolean',
|
|
38
|
+
},
|
|
39
|
+
inspect: { '--input': 'value' },
|
|
40
|
+
validate: {
|
|
41
|
+
'--input': 'value',
|
|
42
|
+
'--skills-dir': 'repeatable',
|
|
43
|
+
'--skill-file': 'repeatable',
|
|
44
|
+
'--allow-extra-skills': 'boolean',
|
|
45
|
+
},
|
|
46
|
+
query: {
|
|
47
|
+
'--input': 'value',
|
|
48
|
+
'--query': 'value',
|
|
49
|
+
'--limit': 'value',
|
|
50
|
+
'--min-score': 'value',
|
|
51
|
+
'--abstention-threshold': 'value',
|
|
52
|
+
},
|
|
53
|
+
migrate: { '--input': 'value', '--output': 'value' },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
interface ParsedOptions {
|
|
57
|
+
readonly values: ReadonlyMap<string, string>;
|
|
58
|
+
readonly repeated: ReadonlyMap<string, readonly string[]>;
|
|
59
|
+
readonly booleans: ReadonlySet<string>;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function runSkillsIndexCommand(
|
|
63
|
+
command: SkillsIndexCommand,
|
|
64
|
+
args: readonly string[],
|
|
65
|
+
operations?: SkillsIndexOperations,
|
|
66
|
+
cwd = process.cwd(),
|
|
67
|
+
): Promise<CommandResult> {
|
|
68
|
+
const options = parseOptions(command, args);
|
|
69
|
+
const api = operations ?? (await loadSkillsIndexOperations(cwd));
|
|
70
|
+
try {
|
|
71
|
+
switch (command) {
|
|
72
|
+
case 'build': {
|
|
73
|
+
const result = await api.buildSkillsIndex({
|
|
74
|
+
directories: repeated(options, '--skills-dir'),
|
|
75
|
+
files: repeated(options, '--skill-file'),
|
|
76
|
+
outputFile: value(options, '--output'),
|
|
77
|
+
threshold: numeric(options, '--threshold'),
|
|
78
|
+
retrievalLimit: numeric(options, '--limit'),
|
|
79
|
+
batchSize: numeric(options, '--batch-size'),
|
|
80
|
+
chunkTokens: numeric(options, '--chunk-tokens'),
|
|
81
|
+
chunkOverlapTokens: numeric(options, '--chunk-overlap'),
|
|
82
|
+
force: options.booleans.has('--force'),
|
|
83
|
+
});
|
|
84
|
+
return resultPresentation(
|
|
85
|
+
result,
|
|
86
|
+
`Built skills index at ${result.outputFile}: ${result.skillCount} skills, ${result.chunkCount} chunks${result.unchanged ? ' (unchanged)' : ''}`,
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
case 'inspect': {
|
|
90
|
+
const result = api.inspectSkillsIndex({
|
|
91
|
+
inputFile: value(options, '--input'),
|
|
92
|
+
cwd,
|
|
93
|
+
});
|
|
94
|
+
return resultPresentation(
|
|
95
|
+
result,
|
|
96
|
+
`Skills index ${result.file}: version ${result.metadata.version}, ${result.metadata.skillCount} skills, ${result.metadata.chunkCount} chunks`,
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
case 'validate': {
|
|
100
|
+
const result = api.validateSkillsIndex({
|
|
101
|
+
inputFile: value(options, '--input'),
|
|
102
|
+
directories: repeated(options, '--skills-dir'),
|
|
103
|
+
files: repeated(options, '--skill-file'),
|
|
104
|
+
allowExtraSkills: options.booleans.has('--allow-extra-skills'),
|
|
105
|
+
cwd,
|
|
106
|
+
});
|
|
107
|
+
if (result.valid) {
|
|
108
|
+
return resultPresentation(result, `Skills index ${result.file} is valid`);
|
|
109
|
+
}
|
|
110
|
+
const drift = result.sourceDrift ?? 'unknown drift';
|
|
111
|
+
return resultPresentation(
|
|
112
|
+
result,
|
|
113
|
+
`Skills index ${result.file} has source drift: ${drift}`,
|
|
114
|
+
1,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
case 'query': {
|
|
118
|
+
const result = await api.querySkillsIndex({
|
|
119
|
+
inputFile: value(options, '--input'),
|
|
120
|
+
query: value(options, '--query') ?? '',
|
|
121
|
+
limit: numeric(options, '--limit'),
|
|
122
|
+
minScore: numeric(options, '--min-score'),
|
|
123
|
+
abstentionThreshold: numeric(options, '--abstention-threshold'),
|
|
124
|
+
cwd,
|
|
125
|
+
});
|
|
126
|
+
if (result.decision === 'selected') {
|
|
127
|
+
return resultPresentation(
|
|
128
|
+
result,
|
|
129
|
+
`Selected ${result.matches.length} skill match(es) from ${result.file}`,
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
return resultPresentation(result, `No skill matches selected from ${result.file}`, 1);
|
|
133
|
+
}
|
|
134
|
+
case 'migrate': {
|
|
135
|
+
const result = api.migrateSkillsIndex({
|
|
136
|
+
inputFile: value(options, '--input'),
|
|
137
|
+
outputFile: value(options, '--output'),
|
|
138
|
+
cwd,
|
|
139
|
+
});
|
|
140
|
+
return resultPresentation(
|
|
141
|
+
result,
|
|
142
|
+
`Migrated skills index ${result.inputFile} from version ${result.fromVersion} to version ${result.toVersion} at ${result.outputFile}`,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
} catch (error) {
|
|
147
|
+
if (!(error instanceof api.SkillsIndexOperationError)) throw error;
|
|
148
|
+
throw operationFailure(error);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function runSkillsIndexBuild(
|
|
153
|
+
args: readonly string[],
|
|
154
|
+
operations?: SkillsIndexOperations,
|
|
155
|
+
cwd?: string,
|
|
156
|
+
) {
|
|
157
|
+
return runSkillsIndexCommand('build', args, operations, cwd);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function runSkillsIndexInspect(
|
|
161
|
+
args: readonly string[],
|
|
162
|
+
operations?: SkillsIndexOperations,
|
|
163
|
+
cwd?: string,
|
|
164
|
+
) {
|
|
165
|
+
return runSkillsIndexCommand('inspect', args, operations, cwd);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function runSkillsIndexValidate(
|
|
169
|
+
args: readonly string[],
|
|
170
|
+
operations?: SkillsIndexOperations,
|
|
171
|
+
cwd?: string,
|
|
172
|
+
) {
|
|
173
|
+
return runSkillsIndexCommand('validate', args, operations, cwd);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function runSkillsIndexQuery(
|
|
177
|
+
args: readonly string[],
|
|
178
|
+
operations?: SkillsIndexOperations,
|
|
179
|
+
cwd?: string,
|
|
180
|
+
) {
|
|
181
|
+
return runSkillsIndexCommand('query', args, operations, cwd);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export function runSkillsIndexMigrate(
|
|
185
|
+
args: readonly string[],
|
|
186
|
+
operations?: SkillsIndexOperations,
|
|
187
|
+
cwd?: string,
|
|
188
|
+
) {
|
|
189
|
+
return runSkillsIndexCommand('migrate', args, operations, cwd);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function parseOptions(command: SkillsIndexCommand, args: readonly string[]): ParsedOptions {
|
|
193
|
+
const specs = OPTION_SPECS[command];
|
|
194
|
+
const values = new Map<string, string>();
|
|
195
|
+
const repeated = new Map<string, string[]>();
|
|
196
|
+
const booleans = new Set<string>();
|
|
197
|
+
for (let position = 0; position < args.length; position++) {
|
|
198
|
+
const token = args[position] ?? '';
|
|
199
|
+
const kind = specs[token];
|
|
200
|
+
if (kind == null) invalidUsage(`Unknown option or argument: ${token}`, token);
|
|
201
|
+
if (kind === 'boolean') {
|
|
202
|
+
if (booleans.has(token)) invalidUsage(`Option may be provided only once: ${token}`, token);
|
|
203
|
+
booleans.add(token);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const optionValue = args[++position];
|
|
207
|
+
if (optionValue == null || optionValue.startsWith('--')) {
|
|
208
|
+
invalidUsage(`Missing value for ${token}`, token);
|
|
209
|
+
}
|
|
210
|
+
if (kind === 'repeatable') {
|
|
211
|
+
repeated.set(token, [...(repeated.get(token) ?? []), optionValue]);
|
|
212
|
+
} else {
|
|
213
|
+
if (values.has(token)) invalidUsage(`Option may be provided only once: ${token}`, token);
|
|
214
|
+
values.set(token, optionValue);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return { values, repeated, booleans };
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function value(options: ParsedOptions, option: string): string | undefined {
|
|
221
|
+
return options.values.get(option);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function repeated(options: ParsedOptions, option: string): readonly string[] | undefined {
|
|
225
|
+
const entries = options.repeated.get(option);
|
|
226
|
+
return entries?.length ? entries : undefined;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
function numeric(options: ParsedOptions, option: string): number | undefined {
|
|
230
|
+
const raw = value(options, option);
|
|
231
|
+
if (raw == null) return undefined;
|
|
232
|
+
const parsed = Number(raw);
|
|
233
|
+
if (!Number.isFinite(parsed)) {
|
|
234
|
+
invalidUsage(`Invalid numeric value for ${option}: ${raw}`, option);
|
|
235
|
+
}
|
|
236
|
+
return parsed;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
function resultPresentation(result: object, text: string, exitCode: 0 | 1 = 0): CommandResult {
|
|
240
|
+
return { data: result as unknown as JsonValue, text, exitCode };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async function loadSkillsIndexOperations(cwd: string): Promise<SkillsIndexOperations> {
|
|
244
|
+
try {
|
|
245
|
+
const projectRequire = createRequire(resolve(cwd, 'package.json'));
|
|
246
|
+
const modulePath = projectRequire.resolve('@di-framework/ai-utils');
|
|
247
|
+
return import(pathToFileURL(modulePath).href);
|
|
248
|
+
} catch (cause) {
|
|
249
|
+
throw new CommandFailure(
|
|
250
|
+
'SKILLS_INDEX_PACKAGE_UNAVAILABLE',
|
|
251
|
+
'Unable to load @di-framework/ai-utils from the current project',
|
|
252
|
+
3,
|
|
253
|
+
{ cause: cause instanceof Error ? cause.message : String(cause) },
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function invalidUsage(message: string, token: string): never {
|
|
259
|
+
throw new CommandFailure('INVALID_USAGE', message, 2, { token });
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function operationFailure(error: SkillsIndexOperationError): CommandFailure {
|
|
263
|
+
const exitCode =
|
|
264
|
+
error.code === 'SOURCE_DRIFT'
|
|
265
|
+
? 1
|
|
266
|
+
: error.code === 'INVALID_OPTIONS' ||
|
|
267
|
+
error.code === 'SOURCE_NOT_FOUND' ||
|
|
268
|
+
error.code === 'INDEX_NOT_FOUND' ||
|
|
269
|
+
error.code === 'INVALID_INDEX'
|
|
270
|
+
? 2
|
|
271
|
+
: 3;
|
|
272
|
+
return new CommandFailure(`SKILLS_INDEX_${error.code}`, error.message, exitCode, {
|
|
273
|
+
operation: error.operation,
|
|
274
|
+
operationCode: error.code,
|
|
275
|
+
});
|
|
276
|
+
}
|