@aeondave/pi-skill-optimizer 3.0.0
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/BENCHMARK.md +354 -0
- package/CHANGELOG.md +117 -0
- package/LICENSE +21 -0
- package/README.md +148 -0
- package/assets/banner.png +0 -0
- package/docs/ARCHITECTURE.md +66 -0
- package/docs/CONFIGURATION.md +138 -0
- package/docs/RANKING.md +155 -0
- package/package.json +85 -0
- package/src/activation.ts +88 -0
- package/src/aliases.ts +111 -0
- package/src/config.ts +446 -0
- package/src/corpus.ts +416 -0
- package/src/evaluation.ts +692 -0
- package/src/generate.ts +365 -0
- package/src/history.ts +147 -0
- package/src/index.ts +1362 -0
- package/src/init-profile.ts +96 -0
- package/src/init-prompt.ts +10 -0
- package/src/init-readiness.ts +123 -0
- package/src/optimize.ts +424 -0
- package/src/output-recovery.ts +19 -0
- package/src/output.ts +629 -0
- package/src/persistence.ts +535 -0
- package/src/profile.ts +253 -0
- package/src/prompts/init-profile.md +40 -0
- package/src/request.ts +304 -0
- package/src/skill-loader.ts +138 -0
- package/src/skills.ts +1230 -0
- package/src/stats.ts +178 -0
- package/src/tools.ts +548 -0
- package/src/usage.ts +256 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { readFileSync, realpathSync, statSync } from "node:fs";
|
|
2
|
+
import { dirname, isAbsolute, relative, resolve, sep } from "node:path";
|
|
3
|
+
import {
|
|
4
|
+
searchSkillCatalog,
|
|
5
|
+
type Skill,
|
|
6
|
+
type SkillSearchOptions,
|
|
7
|
+
type SkillSearchResult,
|
|
8
|
+
} from "./skills.ts";
|
|
9
|
+
|
|
10
|
+
export interface SkillRegistryInput {
|
|
11
|
+
name: string;
|
|
12
|
+
description: string;
|
|
13
|
+
filePath: string;
|
|
14
|
+
baseDir: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface SkillResource {
|
|
18
|
+
name: string;
|
|
19
|
+
description: string;
|
|
20
|
+
content: string;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SkillLocationAction {
|
|
24
|
+
name: string;
|
|
25
|
+
location: string;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface SkillResourceFile {
|
|
29
|
+
name: string;
|
|
30
|
+
relativePath: string;
|
|
31
|
+
content: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SkillRegistryOptions {
|
|
35
|
+
maxResourceBytes?: number;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface SkillRegistry {
|
|
39
|
+
readonly skills: readonly Skill[];
|
|
40
|
+
search(query: string, options?: SkillSearchOptions): SkillSearchResult;
|
|
41
|
+
loadExact(name: string): SkillResource;
|
|
42
|
+
loadResource(name: string, relativePath: string): SkillResourceFile;
|
|
43
|
+
locateExact(name: string): SkillLocationAction;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface RegistryRecord {
|
|
47
|
+
name: string;
|
|
48
|
+
description: string;
|
|
49
|
+
filePath: string;
|
|
50
|
+
baseDir: string;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function containedPath(baseDir: string, filePath: string): string {
|
|
54
|
+
const absoluteBase = resolve(baseDir);
|
|
55
|
+
const candidate = resolve(isAbsolute(filePath) ? filePath : resolve(baseDir, filePath));
|
|
56
|
+
const lexicalRelation = relative(absoluteBase, candidate);
|
|
57
|
+
if (!lexicalRelation || lexicalRelation === ".." || lexicalRelation.startsWith(`..${sep}`) || isAbsolute(lexicalRelation)) {
|
|
58
|
+
throw new Error("Skill resource escapes its registered base directory");
|
|
59
|
+
}
|
|
60
|
+
const realBase = realpathSync.native(absoluteBase);
|
|
61
|
+
const realFile = realpathSync.native(candidate);
|
|
62
|
+
const relation = relative(realBase, realFile);
|
|
63
|
+
if (!relation || relation === ".." || relation.startsWith(`..${sep}`) || isAbsolute(relation)) {
|
|
64
|
+
throw new Error("Skill resource escapes its registered base directory");
|
|
65
|
+
}
|
|
66
|
+
const stats = statSync(realFile);
|
|
67
|
+
if (!stats.isFile()) throw new Error("Skill resource is not a regular file");
|
|
68
|
+
return realFile;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function createSkillRegistry(
|
|
72
|
+
inputs: readonly SkillRegistryInput[],
|
|
73
|
+
options: SkillRegistryOptions = {},
|
|
74
|
+
): SkillRegistry {
|
|
75
|
+
const maxResourceBytes = Math.max(1, Math.trunc(options.maxResourceBytes ?? 1_048_576));
|
|
76
|
+
const records = new Map<string, RegistryRecord>();
|
|
77
|
+
for (const input of inputs) {
|
|
78
|
+
const name = input.name.trim();
|
|
79
|
+
if (!name) throw new TypeError("Skill registry names must be non-empty");
|
|
80
|
+
if (records.has(name)) throw new TypeError(`Duplicate skill registry name: ${name}`);
|
|
81
|
+
records.set(name, {
|
|
82
|
+
name,
|
|
83
|
+
description: input.description,
|
|
84
|
+
filePath: input.filePath,
|
|
85
|
+
baseDir: input.baseDir,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
const skills = Object.freeze([...records.values()].map((record): Skill => Object.freeze({
|
|
89
|
+
name: record.name,
|
|
90
|
+
description: record.description,
|
|
91
|
+
location: "",
|
|
92
|
+
})));
|
|
93
|
+
const getRecord = (name: string): RegistryRecord => {
|
|
94
|
+
const record = records.get(name);
|
|
95
|
+
if (!record) throw new RangeError(`Unknown skill name: ${name}`);
|
|
96
|
+
return record;
|
|
97
|
+
};
|
|
98
|
+
const resolveRecord = (record: RegistryRecord): string => containedPath(record.baseDir, record.filePath);
|
|
99
|
+
const readBounded = (location: string): string => {
|
|
100
|
+
const stats = statSync(location);
|
|
101
|
+
if (stats.size > maxResourceBytes) {
|
|
102
|
+
throw new RangeError(`Skill resource exceeds ${maxResourceBytes} bytes`);
|
|
103
|
+
}
|
|
104
|
+
return readFileSync(location, "utf8");
|
|
105
|
+
};
|
|
106
|
+
return Object.freeze({
|
|
107
|
+
skills,
|
|
108
|
+
search(query: string, searchOptions: SkillSearchOptions = {}): SkillSearchResult {
|
|
109
|
+
return searchSkillCatalog(skills, query, searchOptions);
|
|
110
|
+
},
|
|
111
|
+
loadExact(name: string): SkillResource {
|
|
112
|
+
const record = getRecord(name);
|
|
113
|
+
const location = resolveRecord(record);
|
|
114
|
+
return {
|
|
115
|
+
name: record.name,
|
|
116
|
+
description: record.description,
|
|
117
|
+
content: readBounded(location),
|
|
118
|
+
};
|
|
119
|
+
},
|
|
120
|
+
loadResource(name: string, relativePath: string): SkillResourceFile {
|
|
121
|
+
const record = getRecord(name);
|
|
122
|
+
if (!relativePath || isAbsolute(relativePath)) {
|
|
123
|
+
throw new TypeError("Skill resource path must be relative");
|
|
124
|
+
}
|
|
125
|
+
const skillRoot = dirname(resolveRecord(record));
|
|
126
|
+
const location = containedPath(skillRoot, resolve(skillRoot, relativePath));
|
|
127
|
+
return {
|
|
128
|
+
name: record.name,
|
|
129
|
+
relativePath,
|
|
130
|
+
content: readBounded(location),
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
locateExact(name: string): SkillLocationAction {
|
|
134
|
+
const record = getRecord(name);
|
|
135
|
+
return { name: record.name, location: resolveRecord(record) };
|
|
136
|
+
},
|
|
137
|
+
});
|
|
138
|
+
}
|