@fanglinwei/agent-fit 0.0.0-stage → 0.7.2
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/LICENSE +21 -0
- package/README.md +161 -2
- package/VERSION +1 -0
- package/adapters/codex/modes.json +360 -0
- package/adapters/codex/skills/agent-fit/SKILL.md +120 -0
- package/adapters/codex/skills/agent-fit/agents/openai.yaml +6 -0
- package/adapters/codex/skills/agent-fit/assets/AGENTS.fragment.md +13 -0
- package/adapters/codex/skills/agent-fit/assets/codex/agents/task-delegate.toml +18 -0
- package/adapters/codex/skills/agent-fit/assets/codex/agents/task-execute.toml +20 -0
- package/adapters/codex/skills/agent-fit/assets/codex/agents/task-review.toml +20 -0
- package/adapters/codex/skills/agent-fit/assets/codex/agents/task-scan.toml +20 -0
- package/adapters/codex/skills/agent-fit/assets/codex/config.toml +17 -0
- package/adapters/codex/skills/agent-fit/references/calibration.md +66 -0
- package/adapters/codex/skills/agent-fit/references/configuration.md +42 -0
- package/adapters/codex/skills/agent-fit/references/failure-policy.md +26 -0
- package/adapters/codex/skills/agent-fit/references/task-routing.md +36 -0
- package/bin/agent-fit.mjs +265 -0
- package/docs/README.md +15 -0
- package/docs/implementation-review.md +119 -0
- package/docs/usage.md +481 -0
- package/lib/accounts.mjs +129 -0
- package/lib/codex.mjs +220 -0
- package/lib/install.mjs +796 -0
- package/lib/model-history.mjs +93 -0
- package/lib/models.mjs +376 -0
- package/lib/probe.mjs +186 -0
- package/lib/scopes.mjs +113 -0
- package/lib/server.mjs +200 -0
- package/lib/skill.mjs +145 -0
- package/package.json +15 -4
- package/web/app.js +744 -0
- package/web/index.html +109 -0
- package/web/styles.css +407 -0
package/lib/skill.mjs
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import fs from 'node:fs/promises';
|
|
2
|
+
import { constants } from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
6
|
+
import { skillAssets, VERSION } from './codex.mjs';
|
|
7
|
+
|
|
8
|
+
const MANIFEST = '.agent-fit-manifest.json';
|
|
9
|
+
let testHooks = {};
|
|
10
|
+
export function setSkillTestHooks(hooks = {}) { testHooks = hooks; }
|
|
11
|
+
const hash = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
12
|
+
const fail = (code, message) => { throw Object.assign(new Error(message), { code, exitCode: 1 }); };
|
|
13
|
+
export const sharedSkillPath = () => path.join(os.homedir(), '.agents/skills/agent-fit');
|
|
14
|
+
async function stat(target) { try { return await fs.lstat(target); } catch (error) { if (error.code === 'ENOENT') return null; throw error; } }
|
|
15
|
+
async function checkDirectoryPath(target) {
|
|
16
|
+
let current = path.parse(target).root;
|
|
17
|
+
for (const part of target.slice(current.length).split(path.sep).filter(Boolean)) {
|
|
18
|
+
current = path.join(current, part);
|
|
19
|
+
const info = await stat(current);
|
|
20
|
+
if (info && (!info.isDirectory() || info.isSymbolicLink())) fail('UNSAFE_PATH', `共享 Skill 安装父级不安全:${current}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function rootAt(target) {
|
|
25
|
+
try {
|
|
26
|
+
const root = await fs.realpath(target), info = await fs.lstat(root);
|
|
27
|
+
if (!info.isDirectory() || info.mode & 0o7022 || process.platform !== 'win32' && info.uid !== process.getuid()) fail('UNSAFE_PATH', `Skill 目录所有者、权限或类型不安全:${target}`);
|
|
28
|
+
return root;
|
|
29
|
+
} catch (error) { if (error.code === 'ENOENT' && !await stat(target)) return null; throw error; }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Only the Skill root may be a link. Files and nested directories never follow links.
|
|
33
|
+
export async function readSkillFile(root, relative) {
|
|
34
|
+
if (!relative || relative.split('/').some(part => !part || part === '.' || part === '..') || relative.includes('\\')) fail('UNSAFE_PATH', '非法 Skill 文件路径。');
|
|
35
|
+
if (await fs.realpath(root) !== root) fail('UNSAFE_PATH', `Skill 目录已改变:${root}`);
|
|
36
|
+
let current = root;
|
|
37
|
+
for (const part of relative.split('/')) {
|
|
38
|
+
current = path.join(current, part);
|
|
39
|
+
const info = await stat(current);
|
|
40
|
+
if (!info) return { bytes: null, mode: null };
|
|
41
|
+
if (info.isSymbolicLink() || info.mode & 0o7022 || process.platform !== 'win32' && info.uid !== process.getuid()) fail('UNSAFE_PATH', `Skill 文件路径不安全:${current}`);
|
|
42
|
+
if (current !== path.join(root, relative) && !info.isDirectory()) fail('UNSAFE_PATH', `Skill 父级不是目录:${current}`);
|
|
43
|
+
}
|
|
44
|
+
const handle = await fs.open(current, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
|
|
45
|
+
try {
|
|
46
|
+
const info = await handle.stat();
|
|
47
|
+
if (!info.isFile() || info.size > 1024 * 1024) fail('UNSAFE_PATH', `Skill 不是普通文件或过大:${current}`);
|
|
48
|
+
return { bytes: await handle.readFile(), mode: info.mode & 0o777 };
|
|
49
|
+
} finally { await handle.close(); }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export async function inspectSkill(home) {
|
|
53
|
+
const assets = await skillAssets(), roots = [];
|
|
54
|
+
const shared = await rootAt(sharedSkillPath());
|
|
55
|
+
const legacyPath = path.join(home, 'skills/agent-fit');
|
|
56
|
+
const legacy = await rootAt(legacyPath);
|
|
57
|
+
if ((await stat(legacyPath))?.isSymbolicLink() && legacy !== shared) fail('UNSAFE_PATH', '旧 Skill 软链接必须指向已识别的共享 Skill 目录。');
|
|
58
|
+
for (const root of [...new Set([shared, legacy].filter(Boolean))]) {
|
|
59
|
+
const files = new Map();
|
|
60
|
+
for (const relative of [...assets.keys(), 'references/current-mode.json', MANIFEST]) files.set(relative, await readSkillFile(root, relative));
|
|
61
|
+
const entry = files.get('SKILL.md').bytes;
|
|
62
|
+
if (!entry || !/^name: agent-fit$/m.test(entry.toString('utf8'))) fail('SKILL_CONFLICT', `目录中不是有效的 agent-fit Skill:${root}`);
|
|
63
|
+
roots.push({ root, files });
|
|
64
|
+
}
|
|
65
|
+
const identity = roots.map(({ root, files }) => [root, [...files].map(([relative, value]) => [relative, value.mode, value.bytes === null ? null : hash(value.bytes)])]);
|
|
66
|
+
return { path: roots.length ? path.join(roots[0].root, 'SKILL.md') : null, roots, revision: hash(JSON.stringify(identity)), assets };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export async function updateSkill(home, { dryRun = false, installOnly = false, legacyState = null } = {}) {
|
|
70
|
+
const inspection = await inspectSkill(home), destination = sharedSkillPath();
|
|
71
|
+
const existing = await rootAt(destination);
|
|
72
|
+
if (installOnly && existing) return { result: 'unchanged', path: path.join(existing, 'SKILL.md'), changed_files: [] };
|
|
73
|
+
const root = existing ?? destination, assets = inspection.assets;
|
|
74
|
+
const before = new Map();
|
|
75
|
+
for (const relative of [...assets.keys(), MANIFEST]) before.set(relative, existing ? await readSkillFile(existing, relative) : { bytes: null, mode: null });
|
|
76
|
+
let recorded = {};
|
|
77
|
+
if (before.get(MANIFEST).bytes) {
|
|
78
|
+
try {
|
|
79
|
+
const value = JSON.parse(before.get(MANIFEST).bytes);
|
|
80
|
+
if (value.layout !== 1 || typeof value.version !== 'string' || !value.files || Array.isArray(value.files) || Object.entries(value.files).some(([name, digest]) => !assets.has(name) || !/^[a-f0-9]{64}$/.test(digest))) throw new Error();
|
|
81
|
+
recorded = value.files;
|
|
82
|
+
} catch { fail('SKILL_CONFLICT', '共享 Skill 管理记录无效;保留现有文件。'); }
|
|
83
|
+
}
|
|
84
|
+
const changes = new Map();
|
|
85
|
+
for (const [relative, bytes] of assets) {
|
|
86
|
+
const actual = before.get(relative).bytes;
|
|
87
|
+
if (actual?.equals(bytes)) continue;
|
|
88
|
+
const expected = recorded[relative] ?? legacyState?.legacy_skill_files?.[`skills/agent-fit/${relative}`] ?? legacyState?.files[`skills/agent-fit/${relative}`];
|
|
89
|
+
if (actual !== null && (!expected || hash(actual) !== expected)) fail('SKILL_CONFLICT', `共享 Skill 含未确认的修改,未覆盖:${path.join(root, relative)}`);
|
|
90
|
+
changes.set(relative, bytes);
|
|
91
|
+
}
|
|
92
|
+
const manifest = Buffer.from(JSON.stringify({ layout: 1, version: VERSION, files: Object.fromEntries([...assets].map(([name, bytes]) => [name, hash(bytes)])) }, null, 2) + '\n');
|
|
93
|
+
if (!before.get(MANIFEST).bytes?.equals(manifest)) changes.set(MANIFEST, manifest);
|
|
94
|
+
const report = { action: 'skill-update', home, result: dryRun ? 'preview' : changes.size ? 'applied' : 'unchanged', path: path.join(root, 'SKILL.md'), changed_files: [...changes.keys()], version: VERSION, restart: { required: !dryRun && changes.size > 0, message: dryRun ? '共享规则预览未写入;更新后请重启 Codex。' : changes.size ? '共享规则已更新,请重启 Codex 后使用。' : '共享规则无变化。' } };
|
|
95
|
+
if (!existing) await checkDirectoryPath(path.dirname(root));
|
|
96
|
+
if (dryRun || !changes.size) return report;
|
|
97
|
+
const parent = path.dirname(root), created = [], applied = [];
|
|
98
|
+
await checkDirectoryPath(parent);
|
|
99
|
+
await fs.mkdir(parent, { recursive: true, mode: 0o700 });
|
|
100
|
+
if (await fs.realpath(parent) !== parent) fail('UNSAFE_PATH', `共享 Skill 安装父级含符号链接:${parent}`);
|
|
101
|
+
const lock = path.join(parent, '.agent-fit-skill.lock');
|
|
102
|
+
let handle;
|
|
103
|
+
try { handle = await fs.open(lock, constants.O_WRONLY | constants.O_CREAT | constants.O_EXCL | (constants.O_NOFOLLOW ?? 0), 0o600); }
|
|
104
|
+
catch (error) { if (error.code === 'EEXIST') fail('BUSY', '共享 Skill 正在更新;请稍后重试。'); throw error; }
|
|
105
|
+
const identity = await handle.stat();
|
|
106
|
+
const ownsLock = async () => { const actual = await stat(lock); return actual?.dev === identity.dev && actual?.ino === identity.ino; };
|
|
107
|
+
const write = async (relative, bytes, mode, expected) => {
|
|
108
|
+
if (!await ownsLock()) fail('BUSY', '共享 Skill 更新锁被替换;保留新锁,停止写入。');
|
|
109
|
+
const actual = await readSkillFile(root, relative);
|
|
110
|
+
if (actual.mode !== expected.mode || (actual.bytes === null ? expected.bytes !== null : !actual.bytes.equals(expected.bytes ?? Buffer.alloc(0)))) fail('CONFIG_CHANGED', `共享 Skill 在更新期间改变:${path.join(root, relative)}`);
|
|
111
|
+
const target = path.join(root, relative);
|
|
112
|
+
if (bytes === null) { await fs.unlink(target); return; }
|
|
113
|
+
const temporary = path.join(path.dirname(target), `.agent-fit-${randomUUID()}.tmp`);
|
|
114
|
+
try {
|
|
115
|
+
await fs.writeFile(temporary, bytes, { flag: 'wx', mode });
|
|
116
|
+
await fs.chmod(temporary, mode);
|
|
117
|
+
const current = await readSkillFile(root, relative);
|
|
118
|
+
if (current.mode !== actual.mode || (current.bytes === null ? actual.bytes !== null : !current.bytes.equals(actual.bytes))) fail('CONFIG_CHANGED', `共享 Skill 在替换前改变:${target}`);
|
|
119
|
+
await fs.rename(temporary, target);
|
|
120
|
+
} finally { await fs.unlink(temporary).catch(error => { if (error.code !== 'ENOENT') throw error; }); }
|
|
121
|
+
};
|
|
122
|
+
try {
|
|
123
|
+
const directories = new Set(['']);
|
|
124
|
+
for (const relative of changes.keys()) { let folder = path.posix.dirname(relative); while (folder !== '.') { directories.add(folder); folder = path.posix.dirname(folder); } }
|
|
125
|
+
for (const relative of [...directories].sort((a, b) => a.split('/').length - b.split('/').length)) {
|
|
126
|
+
const target = path.join(root, relative), info = await stat(target);
|
|
127
|
+
if (!info) { await fs.mkdir(target, { mode: 0o700 }); created.push(target); }
|
|
128
|
+
else if (!info.isDirectory() || info.isSymbolicLink()) fail('UNSAFE_PATH', `共享 Skill 目录不安全:${target}`);
|
|
129
|
+
}
|
|
130
|
+
for (const [relative, bytes] of changes) {
|
|
131
|
+
await testHooks.beforeWrite?.(relative, applied.length);
|
|
132
|
+
await write(relative, bytes, before.get(relative).mode ?? 0o600, before.get(relative)); applied.push(relative);
|
|
133
|
+
}
|
|
134
|
+
return report;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
const failures = [];
|
|
137
|
+
for (const relative of applied.reverse()) {
|
|
138
|
+
try { await write(relative, before.get(relative).bytes, before.get(relative).mode, { bytes: changes.get(relative), mode: before.get(relative).mode ?? 0o600 }); }
|
|
139
|
+
catch { failures.push(path.join(root, relative)); }
|
|
140
|
+
}
|
|
141
|
+
for (const target of created.reverse()) await fs.rmdir(target).catch(() => {});
|
|
142
|
+
if (failures.length) fail('SKILL_UPDATE_PARTIAL', `共享 Skill 更新未完成,部分文件回退失败;请核对:${failures.join('、')}`);
|
|
143
|
+
throw error;
|
|
144
|
+
} finally { await handle.close(); if (await ownsLock()) await fs.unlink(lock); }
|
|
145
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,17 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fanglinwei/agent-fit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.7.2",
|
|
4
|
+
"description": "Match delegated tasks to model and reasoning effort profiles for Codex, with scoped configuration and restore.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "fanglinwei",
|
|
8
|
+
"repository": { "type": "git", "url": "https://github.com/fanglinwei/agent-fit.git" },
|
|
9
|
+
"homepage": "https://github.com/fanglinwei/agent-fit#readme",
|
|
10
|
+
"bugs": { "url": "https://github.com/fanglinwei/agent-fit/issues" },
|
|
11
|
+
"keywords": ["codex", "subagents", "model-routing", "reasoning", "skills", "cli"],
|
|
12
|
+
"bin": { "agent-fit": "bin/agent-fit.mjs" },
|
|
13
|
+
"engines": { "node": ">=22" },
|
|
14
|
+
"files": ["bin/", "lib/", "web/", "adapters/", "VERSION", "README.md", "docs/README.md", "docs/usage.md", "docs/implementation-review.md", "LICENSE"],
|
|
15
|
+
"scripts": { "test": "node --test tests/*.test.mjs", "test:browser": "node tests/browser.mjs" },
|
|
16
|
+
"dependencies": { "smol-toml": "1.9.1" }
|
|
17
|
+
}
|