@tyrese_guo_peng/recastflow-adapters 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/THIRD_PARTY_NOTICES.md +30 -0
- package/dist/bootstrap.d.ts +65 -0
- package/dist/bootstrap.js +273 -0
- package/dist/bootstrap.js.map +1 -0
- package/dist/claude-code.d.ts +81 -0
- package/dist/claude-code.js +331 -0
- package/dist/claude-code.js.map +1 -0
- package/dist/flow.d.ts +195 -0
- package/dist/flow.js +327 -0
- package/dist/flow.js.map +1 -0
- package/dist/github.d.ts +40 -0
- package/dist/github.js +85 -0
- package/dist/github.js.map +1 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.js +26 -0
- package/dist/index.js.map +1 -0
- package/dist/opencode.d.ts +37 -0
- package/dist/opencode.js +242 -0
- package/dist/opencode.js.map +1 -0
- package/dist/projects.d.ts +17 -0
- package/dist/projects.js +57 -0
- package/dist/projects.js.map +1 -0
- package/dist/registry.d.ts +3 -0
- package/dist/registry.js +8 -0
- package/dist/registry.js.map +1 -0
- package/dist/shared.d.ts +46 -0
- package/dist/shared.js +211 -0
- package/dist/shared.js.map +1 -0
- package/dist/templates.d.ts +133 -0
- package/dist/templates.js +291 -0
- package/dist/templates.js.map +1 -0
- package/package.json +23 -3
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 TyresePeng
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
- The strict skill guidance in `packages/adapters/src/bootstrap.ts` adapts wording
|
|
4
|
+
from [Superpowers’ `using-superpowers` skill](https://github.com/obra/superpowers/blob/main/skills/using-superpowers/SKILL.md).
|
|
5
|
+
Copyright (c) 2025 Jesse Vincent.
|
|
6
|
+
- The hook signature examples in `docs/superpowers/spikes/2026-11-18-hooks-spike.md`
|
|
7
|
+
are summarized from the [OpenCode plugin types](https://github.com/anomalyco/opencode/tree/dev/packages/plugin).
|
|
8
|
+
Copyright (c) 2025 opencode.
|
|
9
|
+
|
|
10
|
+
The following MIT license applies to the material identified above:
|
|
11
|
+
|
|
12
|
+
```text
|
|
13
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
14
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
15
|
+
in the Software without restriction, including without limitation the rights
|
|
16
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
17
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
18
|
+
furnished to do so, subject to the following conditions:
|
|
19
|
+
|
|
20
|
+
The above copyright notice and this permission notice shall be included in all
|
|
21
|
+
copies or substantial portions of the Software.
|
|
22
|
+
|
|
23
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
24
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
25
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
26
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
27
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
28
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
29
|
+
SOFTWARE.
|
|
30
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export type BootstrapIntensity = 'normal' | 'strict';
|
|
2
|
+
export type BootstrapDefaultIntensity = 'off' | BootstrapIntensity;
|
|
3
|
+
export type BootstrapState = {
|
|
4
|
+
default: BootstrapDefaultIntensity;
|
|
5
|
+
skills: Record<string, BootstrapIntensity>;
|
|
6
|
+
};
|
|
7
|
+
export type ResolvedBootstrapSkill = {
|
|
8
|
+
name: string;
|
|
9
|
+
description: string;
|
|
10
|
+
intensity: BootstrapIntensity;
|
|
11
|
+
};
|
|
12
|
+
/** 强度状态文件(相对 dataRoot)——与 library.tombstones/disabled 同目录同纪律。 */
|
|
13
|
+
export declare const BOOTSTRAP_STATE_FILE = "library.intensity.json";
|
|
14
|
+
export declare const BOOTSTRAP_AGENTS_START = "<!-- CAESARFLOW_SKILLS_START -->";
|
|
15
|
+
export declare const BOOTSTRAP_AGENTS_END = "<!-- CAESARFLOW_SKILLS_END -->";
|
|
16
|
+
/** 注入清单上限:引导文本是每会话常驻上下文,必须封顶(超出省略并提示)。 */
|
|
17
|
+
export declare const MAX_BOOTSTRAP_SKILLS = 20;
|
|
18
|
+
/** 读强度状态:缺失/损坏按 `{ default: 'normal', skills: {} }` 兜底(与
|
|
19
|
+
* 墓碑/禁用文件同纪律——状态文件绝不阻断主流程)。 */
|
|
20
|
+
export declare function readBootstrapState(dataRoot: string): BootstrapState;
|
|
21
|
+
export declare function writeBootstrapState(dataRoot: string, state: BootstrapState): void;
|
|
22
|
+
/**
|
|
23
|
+
* 解析生效强度:技能级覆盖 > 全局默认;default 'off' 直接返回空(全局关
|
|
24
|
+
* 闭语义可预测——不因个别覆盖而复活)。结果按名称排序(注入顺序稳定)。
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveBootstrapSkills(state: BootstrapState, deployed: Array<{
|
|
27
|
+
name: string;
|
|
28
|
+
description: string;
|
|
29
|
+
}>): ResolvedBootstrapSkill[];
|
|
30
|
+
/** claude SessionStart hook 的 additionalContext 正文;空清单返回 ''(脚本据此静默)。 */
|
|
31
|
+
export declare function renderBootstrapContext(resolved: ResolvedBootstrapSkill[]): string;
|
|
32
|
+
/** opencode AGENTS.md 托管段:标记恒在场(off/空清单为一行占位),upsert
|
|
33
|
+
* 幂等且让用户知道段落的来历。 */
|
|
34
|
+
export declare function renderOpencodeAgentsBlock(resolved: ResolvedBootstrapSkill[]): string;
|
|
35
|
+
/** 标记段 upsert:无段 → 追加到文件尾部;有段 → 原位整体替换;段后内容保留。 */
|
|
36
|
+
export declare function upsertManagedBlock(content: string, block: string): string;
|
|
37
|
+
/** 移除托管段;文件因此变空时调用方负责删除文件。 */
|
|
38
|
+
export declare function stripManagedBlock(content: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* 重写 opencode 侧 AGENTS.md 托管段:读 `<home>/.config/opencode/skills/`
|
|
41
|
+
* 的已部署技能 + dataRoot 强度状态 → 渲染 → upsert。技能部署/启停(部署
|
|
42
|
+
* 回收后)/删除、强度与默认档变更后由 server 调用;bind 时初始化。
|
|
43
|
+
* `dryRun` 只计算不落盘(bind 用它判定是否需要备份 AGENTS.md)。
|
|
44
|
+
*/
|
|
45
|
+
export declare function syncOpencodeBootstrap(options: {
|
|
46
|
+
home: string;
|
|
47
|
+
dataRoot: string;
|
|
48
|
+
dryRun?: boolean;
|
|
49
|
+
}): {
|
|
50
|
+
written: boolean;
|
|
51
|
+
skills: number;
|
|
52
|
+
};
|
|
53
|
+
/** unbind/关闭时移除托管段;文件因此变空则整个删除(bind 前不存在的文件不留残骸)。 */
|
|
54
|
+
export declare function removeOpencodeBootstrap(options: {
|
|
55
|
+
home: string;
|
|
56
|
+
}): {
|
|
57
|
+
written: boolean;
|
|
58
|
+
};
|
|
59
|
+
/**
|
|
60
|
+
* claude SessionStart hook 脚本(自包含 .mjs,bind 落到
|
|
61
|
+
* `<dataRoot>/runtime/bootstrap.mjs`;command 带 caesarflow:bootstrap 指纹
|
|
62
|
+
* 供 owned 识别)。每次会话动态读部署目录与强度状态——永不过期,零维护。
|
|
63
|
+
* 容错纪律:任何异常静默退出(exit 0 无输出),绝不阻断会话启动。
|
|
64
|
+
*/
|
|
65
|
+
export declare const BOOTSTRAP_MJS_SOURCE: string;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file 技能自动引导(skill auto-bootstrap):让部署到用户级 CLI 目录的技能
|
|
3
|
+
* 在会话启动时被主动发现与使用——借 superpowers 验证过的机制(SessionStart
|
|
4
|
+
* hook 的 additionalContext 注入 + 指令文件托管段),按强度分级渲染。
|
|
5
|
+
*
|
|
6
|
+
* 两端通道:
|
|
7
|
+
* - claude-code:bind 落地 {@link BOOTSTRAP_MJS_SOURCE}(自包含 hook 脚本,
|
|
8
|
+
* 每次会话动态读 `~/.claude/skills/` + 强度状态文件),输出
|
|
9
|
+
* hookSpecificOutput.additionalContext——与 DeployDrawer 零漂移同理,脚本
|
|
10
|
+
* 自包含避免运行时 import 失败阻断会话启动。
|
|
11
|
+
* - opencode:`~/.config/opencode/AGENTS.md` 的 `CAESARFLOW_SKILLS` 托管段
|
|
12
|
+
* (标记注释包裹,只动段内内容;CodeGraph 等他人段不受影响)——opencode
|
|
13
|
+
* 没有等价的 additionalContext hook,指令文件是其官方注入通道;技能部署/
|
|
14
|
+
* 启停/删除后由 server 调 {@link syncOpencodeBootstrap} 重写段落。
|
|
15
|
+
*
|
|
16
|
+
* 强度模型:全局默认档 off/normal/strict(off = 一键关闭,技能级覆盖也
|
|
17
|
+
* 失效——「关」的语义必须可预测);技能级 normal/strict 覆盖默认档。
|
|
18
|
+
* strict 措辞对齐 superpowers(1% 适用必须 invoke);normal 是温和版
|
|
19
|
+
* (任务开始前检查,命中再 invoke)。
|
|
20
|
+
*/
|
|
21
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
|
|
22
|
+
import { dirname, join } from 'node:path';
|
|
23
|
+
import { listLocalSkills, LOCAL_SKILLS_DIR, parseSkillMarkdown } from './templates.js';
|
|
24
|
+
/** 强度状态文件(相对 dataRoot)——与 library.tombstones/disabled 同目录同纪律。 */
|
|
25
|
+
export const BOOTSTRAP_STATE_FILE = 'library.intensity.json';
|
|
26
|
+
export const BOOTSTRAP_AGENTS_START = '<!-- CAESARFLOW_SKILLS_START -->';
|
|
27
|
+
export const BOOTSTRAP_AGENTS_END = '<!-- CAESARFLOW_SKILLS_END -->';
|
|
28
|
+
/** 注入清单上限:引导文本是每会话常驻上下文,必须封顶(超出省略并提示)。 */
|
|
29
|
+
export const MAX_BOOTSTRAP_SKILLS = 20;
|
|
30
|
+
const DEFAULT_STATE = { default: 'normal', skills: {} };
|
|
31
|
+
function isObject(value) {
|
|
32
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
33
|
+
}
|
|
34
|
+
/** 读强度状态:缺失/损坏按 `{ default: 'normal', skills: {} }` 兜底(与
|
|
35
|
+
* 墓碑/禁用文件同纪律——状态文件绝不阻断主流程)。 */
|
|
36
|
+
export function readBootstrapState(dataRoot) {
|
|
37
|
+
try {
|
|
38
|
+
const raw = JSON.parse(readFileSync(join(dataRoot, BOOTSTRAP_STATE_FILE), 'utf8'));
|
|
39
|
+
if (!isObject(raw))
|
|
40
|
+
return { ...DEFAULT_STATE, skills: {} };
|
|
41
|
+
const fallback = raw.default === 'off' || raw.default === 'strict' ? raw.default : 'normal';
|
|
42
|
+
const skills = {};
|
|
43
|
+
if (isObject(raw.skills)) {
|
|
44
|
+
for (const [name, value] of Object.entries(raw.skills)) {
|
|
45
|
+
if (value === 'normal' || value === 'strict')
|
|
46
|
+
skills[name] = value;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { default: fallback, skills };
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return { ...DEFAULT_STATE, skills: {} };
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
export function writeBootstrapState(dataRoot, state) {
|
|
56
|
+
mkdirSync(dataRoot, { recursive: true });
|
|
57
|
+
writeFileSync(join(dataRoot, BOOTSTRAP_STATE_FILE), JSON.stringify(state, null, 2) + '\n', 'utf8');
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* 解析生效强度:技能级覆盖 > 全局默认;default 'off' 直接返回空(全局关
|
|
61
|
+
* 闭语义可预测——不因个别覆盖而复活)。结果按名称排序(注入顺序稳定)。
|
|
62
|
+
*/
|
|
63
|
+
export function resolveBootstrapSkills(state, deployed) {
|
|
64
|
+
if (state.default === 'off')
|
|
65
|
+
return [];
|
|
66
|
+
return deployed
|
|
67
|
+
.map((skill) => ({ ...skill, intensity: state.skills[skill.name] ?? state.default }))
|
|
68
|
+
.sort((a, b) => a.name.localeCompare(b.name));
|
|
69
|
+
}
|
|
70
|
+
function bootstrapSections(resolved) {
|
|
71
|
+
const sorted = [...resolved].sort((a, b) => a.name.localeCompare(b.name));
|
|
72
|
+
const capped = sorted.slice(0, MAX_BOOTSTRAP_SKILLS);
|
|
73
|
+
const line = (skill) => `- ${skill.name}: ${skill.description}`;
|
|
74
|
+
return {
|
|
75
|
+
intro: 'The following skills are deployed in this environment and available via the Skill tool. They are maintained project assets - prefer them over improvising an approach.',
|
|
76
|
+
normal: capped.filter((skill) => skill.intensity === 'normal').map(line),
|
|
77
|
+
strict: capped.filter((skill) => skill.intensity === 'strict').map(line),
|
|
78
|
+
omitted: sorted.length - capped.length,
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/** claude SessionStart hook 的 additionalContext 正文;空清单返回 ''(脚本据此静默)。 */
|
|
82
|
+
export function renderBootstrapContext(resolved) {
|
|
83
|
+
if (resolved.length === 0)
|
|
84
|
+
return '';
|
|
85
|
+
const { intro, normal, strict, omitted } = bootstrapSections(resolved);
|
|
86
|
+
const lines = ['<caesarflow-skills>', intro];
|
|
87
|
+
if (normal.length > 0) {
|
|
88
|
+
lines.push('', 'Before starting a task, check whether one applies. If a skill matches the task (or the user names it), invoke it via the Skill tool first:');
|
|
89
|
+
lines.push(...normal);
|
|
90
|
+
}
|
|
91
|
+
if (strict.length > 0) {
|
|
92
|
+
lines.push('', 'MUST invoke before ANY response or action (including clarifying questions) if there is even a small chance they apply:');
|
|
93
|
+
lines.push(...strict);
|
|
94
|
+
}
|
|
95
|
+
if (omitted > 0)
|
|
96
|
+
lines.push('', `(${omitted} more deployed skills omitted - list them with the Skill tool if needed)`);
|
|
97
|
+
lines.push('</caesarflow-skills>');
|
|
98
|
+
return lines.join('\n');
|
|
99
|
+
}
|
|
100
|
+
/** opencode AGENTS.md 托管段:标记恒在场(off/空清单为一行占位),upsert
|
|
101
|
+
* 幂等且让用户知道段落的来历。 */
|
|
102
|
+
export function renderOpencodeAgentsBlock(resolved) {
|
|
103
|
+
const lines = [BOOTSTRAP_AGENTS_START, '## recastflow deployed skills'];
|
|
104
|
+
if (resolved.length === 0) {
|
|
105
|
+
lines.push('', 'Skill auto-bootstrap is disabled or no skills are deployed yet.');
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
const { intro, normal, strict, omitted } = bootstrapSections(resolved);
|
|
109
|
+
lines.push('', intro);
|
|
110
|
+
if (normal.length > 0) {
|
|
111
|
+
lines.push('', 'Before starting a task, check whether one applies. If a skill matches the task (or the user names it), invoke it via the skill tool first:');
|
|
112
|
+
lines.push(...normal);
|
|
113
|
+
}
|
|
114
|
+
if (strict.length > 0) {
|
|
115
|
+
lines.push('', 'MUST invoke before any response or action (including clarifying questions) if there is even a small chance they apply:');
|
|
116
|
+
lines.push(...strict);
|
|
117
|
+
}
|
|
118
|
+
if (omitted > 0)
|
|
119
|
+
lines.push('', `(${omitted} more deployed skills omitted - list them with the skill tool if needed)`);
|
|
120
|
+
}
|
|
121
|
+
lines.push(BOOTSTRAP_AGENTS_END);
|
|
122
|
+
return lines.join('\n');
|
|
123
|
+
}
|
|
124
|
+
/** 标记段 upsert:无段 → 追加到文件尾部;有段 → 原位整体替换;段后内容保留。 */
|
|
125
|
+
export function upsertManagedBlock(content, block) {
|
|
126
|
+
const start = content.indexOf(BOOTSTRAP_AGENTS_START);
|
|
127
|
+
const parts = [];
|
|
128
|
+
const head = (start === -1 ? content : content.slice(0, start)).replace(/\s+$/, '');
|
|
129
|
+
if (head.length > 0)
|
|
130
|
+
parts.push(head);
|
|
131
|
+
parts.push(block);
|
|
132
|
+
let tail = '';
|
|
133
|
+
if (start !== -1) {
|
|
134
|
+
const end = content.indexOf(BOOTSTRAP_AGENTS_END, start);
|
|
135
|
+
tail = end === -1 ? '' : content.slice(end + BOOTSTRAP_AGENTS_END.length).replace(/^\s+/, '').replace(/\s+$/, '');
|
|
136
|
+
}
|
|
137
|
+
if (tail.length > 0)
|
|
138
|
+
parts.push(tail);
|
|
139
|
+
return `${parts.join('\n\n')}\n`;
|
|
140
|
+
}
|
|
141
|
+
/** 移除托管段;文件因此变空时调用方负责删除文件。 */
|
|
142
|
+
export function stripManagedBlock(content) {
|
|
143
|
+
const start = content.indexOf(BOOTSTRAP_AGENTS_START);
|
|
144
|
+
if (start === -1)
|
|
145
|
+
return content;
|
|
146
|
+
const end = content.indexOf(BOOTSTRAP_AGENTS_END, start);
|
|
147
|
+
const head = content.slice(0, start).replace(/\s+$/, '');
|
|
148
|
+
const tail = end === -1 ? '' : content.slice(end + BOOTSTRAP_AGENTS_END.length).replace(/^\s+/, '').replace(/\s+$/, '');
|
|
149
|
+
const parts = [head, tail].filter((part) => part.length > 0);
|
|
150
|
+
return parts.length === 0 ? '' : `${parts.join('\n\n')}\n`;
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* 重写 opencode 侧 AGENTS.md 托管段:读 `<home>/.config/opencode/skills/`
|
|
154
|
+
* 的已部署技能 + dataRoot 强度状态 → 渲染 → upsert。技能部署/启停(部署
|
|
155
|
+
* 回收后)/删除、强度与默认档变更后由 server 调用;bind 时初始化。
|
|
156
|
+
* `dryRun` 只计算不落盘(bind 用它判定是否需要备份 AGENTS.md)。
|
|
157
|
+
*/
|
|
158
|
+
export function syncOpencodeBootstrap(options) {
|
|
159
|
+
const state = readBootstrapState(options.dataRoot);
|
|
160
|
+
const opencodeRoot = join(options.home, '.config', 'opencode');
|
|
161
|
+
const deployed = [];
|
|
162
|
+
if (existsSync(join(opencodeRoot, LOCAL_SKILLS_DIR))) {
|
|
163
|
+
for (const name of listLocalSkills(opencodeRoot)) {
|
|
164
|
+
const text = readFileSync(join(opencodeRoot, LOCAL_SKILLS_DIR, name, 'SKILL.md'), 'utf8');
|
|
165
|
+
deployed.push({ name, description: parseSkillMarkdown(text, name).description });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
const resolved = resolveBootstrapSkills(state, deployed);
|
|
169
|
+
const agentsFile = join(opencodeRoot, 'AGENTS.md');
|
|
170
|
+
const existing = existsSync(agentsFile) ? readFileSync(agentsFile, 'utf8') : '';
|
|
171
|
+
const next = upsertManagedBlock(existing, renderOpencodeAgentsBlock(resolved));
|
|
172
|
+
if (next === existing || options.dryRun === true)
|
|
173
|
+
return { written: next !== existing, skills: resolved.length };
|
|
174
|
+
mkdirSync(dirname(agentsFile), { recursive: true });
|
|
175
|
+
writeFileSync(agentsFile, next, 'utf8');
|
|
176
|
+
return { written: true, skills: resolved.length };
|
|
177
|
+
}
|
|
178
|
+
/** unbind/关闭时移除托管段;文件因此变空则整个删除(bind 前不存在的文件不留残骸)。 */
|
|
179
|
+
export function removeOpencodeBootstrap(options) {
|
|
180
|
+
const agentsFile = join(options.home, '.config', 'opencode', 'AGENTS.md');
|
|
181
|
+
if (!existsSync(agentsFile))
|
|
182
|
+
return { written: false };
|
|
183
|
+
const existing = readFileSync(agentsFile, 'utf8');
|
|
184
|
+
const next = stripManagedBlock(existing);
|
|
185
|
+
if (next === existing)
|
|
186
|
+
return { written: false };
|
|
187
|
+
if (next.trim().length === 0) {
|
|
188
|
+
rmSync(agentsFile, { force: true });
|
|
189
|
+
return { written: true };
|
|
190
|
+
}
|
|
191
|
+
writeFileSync(agentsFile, next, 'utf8');
|
|
192
|
+
return { written: true };
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* claude SessionStart hook 脚本(自包含 .mjs,bind 落到
|
|
196
|
+
* `<dataRoot>/runtime/bootstrap.mjs`;command 带 caesarflow:bootstrap 指纹
|
|
197
|
+
* 供 owned 识别)。每次会话动态读部署目录与强度状态——永不过期,零维护。
|
|
198
|
+
* 容错纪律:任何异常静默退出(exit 0 无输出),绝不阻断会话启动。
|
|
199
|
+
*/
|
|
200
|
+
export const BOOTSTRAP_MJS_SOURCE = String.raw `#!/usr/bin/env node
|
|
201
|
+
// caesarflow:bootstrap — SessionStart additionalContext injector (skill auto-bootstrap)
|
|
202
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
203
|
+
import { homedir } from 'node:os'
|
|
204
|
+
import { join } from 'node:path'
|
|
205
|
+
|
|
206
|
+
const MAX_SKILLS = 20
|
|
207
|
+
try {
|
|
208
|
+
const home = homedir()
|
|
209
|
+
const claudeSkillsDir = join(home, '.claude', 'skills')
|
|
210
|
+
const dataRoot = process.env.CAESARFLOW_HOME ?? join(home, '.caesarflow')
|
|
211
|
+
let defaultIntensity = 'normal'
|
|
212
|
+
const overrides = {}
|
|
213
|
+
try {
|
|
214
|
+
const raw = JSON.parse(readFileSync(join(dataRoot, 'library.intensity.json'), 'utf8'))
|
|
215
|
+
if (raw && (raw.default === 'off' || raw.default === 'strict' || raw.default === 'normal')) defaultIntensity = raw.default
|
|
216
|
+
if (raw && raw.skills && typeof raw.skills === 'object' && !Array.isArray(raw.skills)) {
|
|
217
|
+
for (const [name, value] of Object.entries(raw.skills)) {
|
|
218
|
+
if (value === 'normal' || value === 'strict') overrides[name] = value
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
} catch {}
|
|
222
|
+
if (defaultIntensity === 'off' || !existsSync(claudeSkillsDir)) process.exit(0)
|
|
223
|
+
|
|
224
|
+
const deployed = []
|
|
225
|
+
for (const entry of readdirSync(claudeSkillsDir, { withFileTypes: true })) {
|
|
226
|
+
if (!entry.isDirectory() || entry.name.startsWith('.')) continue
|
|
227
|
+
const file = join(claudeSkillsDir, entry.name, 'SKILL.md')
|
|
228
|
+
if (!existsSync(file)) continue
|
|
229
|
+
let description = ''
|
|
230
|
+
try {
|
|
231
|
+
const lines = readFileSync(file, 'utf8').split('\n')
|
|
232
|
+
let start = 0
|
|
233
|
+
if (lines[0] && lines[0].trim() === '---') {
|
|
234
|
+
const close = lines.slice(1).findIndex((line) => line.trim() === '---')
|
|
235
|
+
if (close !== -1) {
|
|
236
|
+
for (const line of lines.slice(1, close + 1)) {
|
|
237
|
+
const idx = line.indexOf(':')
|
|
238
|
+
if (idx === -1) continue
|
|
239
|
+
const key = line.slice(0, idx).trim()
|
|
240
|
+
const value = line.slice(idx + 1).trim().replace(/^['"]|['"]$/g, '')
|
|
241
|
+
if (key === 'description' && value.length > 0) { description = value; break }
|
|
242
|
+
}
|
|
243
|
+
start = close + 2
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (description.length === 0) {
|
|
247
|
+
const first = lines.slice(start).map((line) => line.trim()).find((line) => line.length > 0)
|
|
248
|
+
if (first !== undefined) description = first.replace(/^#+\s*/, '').slice(0, 100)
|
|
249
|
+
}
|
|
250
|
+
} catch {}
|
|
251
|
+
deployed.push({ name: entry.name, description: description.length > 0 ? description : entry.name, intensity: overrides[entry.name] ?? defaultIntensity })
|
|
252
|
+
}
|
|
253
|
+
deployed.sort((a, b) => a.name.localeCompare(b.name))
|
|
254
|
+
if (deployed.length === 0) process.exit(0)
|
|
255
|
+
const capped = deployed.slice(0, MAX_SKILLS)
|
|
256
|
+
const line = (skill) => '- ' + skill.name + ': ' + skill.description
|
|
257
|
+
const normal = capped.filter((skill) => skill.intensity === 'normal').map(line)
|
|
258
|
+
const strict = capped.filter((skill) => skill.intensity === 'strict').map(line)
|
|
259
|
+
const lines = ['<caesarflow-skills>', 'The following skills are deployed in this environment and available via the Skill tool. They are maintained project assets - prefer them over improvising an approach.']
|
|
260
|
+
if (normal.length > 0) {
|
|
261
|
+
lines.push('', 'Before starting a task, check whether one applies. If a skill matches the task (or the user names it), invoke it via the Skill tool first:', ...normal)
|
|
262
|
+
}
|
|
263
|
+
if (strict.length > 0) {
|
|
264
|
+
lines.push('', 'MUST invoke before ANY response or action (including clarifying questions) if there is even a small chance they apply:', ...strict)
|
|
265
|
+
}
|
|
266
|
+
if (deployed.length > capped.length) lines.push('', '(' + (deployed.length - capped.length) + ' more deployed skills omitted - list them with the Skill tool if needed)')
|
|
267
|
+
lines.push('</caesarflow-skills>')
|
|
268
|
+
process.stdout.write(JSON.stringify({
|
|
269
|
+
hookSpecificOutput: { hookEventName: 'SessionStart', additionalContext: lines.join('\n') },
|
|
270
|
+
}))
|
|
271
|
+
} catch {}
|
|
272
|
+
`;
|
|
273
|
+
//# sourceMappingURL=bootstrap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bootstrap.js","sourceRoot":"","sources":["../src/bootstrap.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,OAAO,EAAE,UAAU,EA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AE,CAAA;AACnD,CAAC;AAED,qDAAqD;AACrD,MAAM,UAAU,uBAAuB,CAAC,OAAyB;IAC/D,MAAM,UAAU,GAAG,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,SAAS,EAAE,UAAU,EAAE,WAAW,CAAC,CAAA;IACzE,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA;IACtD,MAAM,QAAQ,GAAG,YAAY,CAAC,UAAU,EAAE,MAAM,CAAC,CAAA;IACjD,MAAM,IAAI,GAAG,iBAAiB,CAAC,QAAQ,CAAC,CAAA;IACxC,IAAI,IAAI,KAAK,QAAQ;QAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA;IAChD,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAC7B,MAAM,CAAC,UAAU,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAA;QACnC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAA;IAC1B,CAAC;IACD,aAAa,CAAC,UAAU,EAAE,IAAI,EAAE,MAAM,CAAC,CAAA;IACvC,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,CAAA;AAC1B,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,MAAM,CAAC,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAwE7C,CAAA"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import type { CliAdapter, CompiledFile } from '@tyrese_guo_peng/recastflow-core';
|
|
2
|
+
/**
|
|
3
|
+
* claude-code hook 上报脚本源码(编译产物,写入 `.claude/hooks/caesarflow-report.mjs`,
|
|
4
|
+
* 由 settings.json 的 command hook 以 `node .claude/hooks/caesarflow-report.mjs` 调起)。
|
|
5
|
+
*
|
|
6
|
+
* v2(Plan 6 观察台):在既有节点事件(SubagentStart/SubagentStop 与
|
|
7
|
+
* PreToolUse(Task|Agent) 兼容兜底)之上,PreToolUse/PostToolUse 拦截**全量工具**
|
|
8
|
+
* → tool_start/tool_done。元数据粒度(锁定决策:标准元数据,对话/文件内容永不
|
|
9
|
+
* 进系统):
|
|
10
|
+
* - Write/Edit/Read/NotebookEdit 等 file 类工具 → path(tool_input 的
|
|
11
|
+
* file_path/notebook_path,取先命中者);
|
|
12
|
+
* - Bash → command 截断 200 字符(隐私边界:只留命令摘要,不留全文);
|
|
13
|
+
* - 其余工具只写工具名。
|
|
14
|
+
* - Task/Agent(spawn 工具)**不写 tool 事件**:节点生命周期由 node_start/
|
|
15
|
+
* node_done 专属路径记录(且两者在同一 PreToolUse 会重叠),再写 tool 事件
|
|
16
|
+
* 会在时间线上产生双重记录。
|
|
17
|
+
* - tool_done 只写工具名、不含 ok/fail:PostToolUse 载荷无可靠结果状态
|
|
18
|
+
* (tool_response 形状随版本漂移,判定失败会把猜测写进日志)——失败可见性
|
|
19
|
+
* 是已知局限,留待 Plan 3 的 `recastflow hook` 统一协议时补。
|
|
20
|
+
*
|
|
21
|
+
* deploy 与 observe 安装的是同一份脚本("安装即监控")。JSON 解析栈与 v1
|
|
22
|
+
* 相同(node 内置 JSON.parse,不引入 jq/bash 依赖)。
|
|
23
|
+
*
|
|
24
|
+
* 边界说明:这是 Plan 2 的独立过渡脚本——Plan 3 将以 `recastflow hook` 子命令取代
|
|
25
|
+
* (stdin JSON 协议相同;node_done 与最近 node_start 的配对归属逻辑在彼处完善)。
|
|
26
|
+
*
|
|
27
|
+
* 真机验证遗留 TODO:hooks spike(docs/superpowers/spikes/2026-11-18-hooks-spike.md)
|
|
28
|
+
* 对本通道仅有文档级验证;v2 的 tool 事件通道已由测试以 node 真执行驱动 stdin
|
|
29
|
+
* JSON 覆盖(claude-code.test.ts HOOK_SCRIPT v2 组),但真实 claude-code 会话中
|
|
30
|
+
* 的端到端验证仍待用户实测(Plan 6 Task 8)。
|
|
31
|
+
*
|
|
32
|
+
* 防御性约定(hooks 绝不能破坏宿主 CLI):stdin 读取、JSON 解析、事件映射、落盘
|
|
33
|
+
* 任一环节失败都静默 exit 0,不产生 stderr 输出。
|
|
34
|
+
*/
|
|
35
|
+
export declare const HOOK_SCRIPT = "#!/usr/bin/env node\n// recastflow hook emitter (v2: full-tool telemetry + node events).\n// - Node events: SubagentStart/SubagentStop (preferred) + PreToolUse on Task|Agent\n// as fallback for older CLIs without Subagent* events.\n// - Tool events (Plan 6 observatory): PreToolUse/PostToolUse on ALL tools emit\n// tool_start/tool_done. Metadata is deliberately minimal (privacy boundary):\n// file-class tools report only the target path, Bash reports the command\n// truncated to 200 chars, everything else reports just the tool name. Conversation\n// and file CONTENT never enter the system.\n// - Spawn tools (Task/Agent) never emit tool events: their lifecycle is owned by\n// node_start/node_done (and both fire on the same PreToolUse) \u2014 a tool event\n// would double-record on the timeline.\n// - tool_done carries no ok/fail flag: the PostToolUse payload has no reliable\n// status signal; recording a guess would corrupt the log. Known limitation.\n// Real-machine verification is still pending (docs/superpowers/spikes/2026-11-18-hooks-spike.md).\n// Hooks must never break the host CLI: any error is swallowed, exit code stays 0.\nimport { appendFile, mkdir } from 'node:fs/promises'\nimport { join } from 'node:path'\n\n// Privacy boundary: command metadata is a summary, never the full transcript.\nconst COMMAND_LIMIT = 200\n// File-class tools carry the target path under one of these keys (varies across CLI versions).\nconst FILE_PATH_KEYS = ['file_path', 'notebook_path']\n\n// Extract path/command metadata for a tool_start event. File-class path wins over\n// command (the two key sets never co-occur in practice); anything else reports no metadata.\nconst metadataOf = (toolName, toolInput) => {\n if (!toolInput || typeof toolInput !== 'object') return {}\n for (const key of FILE_PATH_KEYS) {\n const value = toolInput[key]\n if (typeof value === 'string' && value.length > 0) return { path: value }\n }\n if (toolName === 'Bash') {\n const value = toolInput.command\n if (typeof value === 'string' && value.length > 0) return { command: value.slice(0, COMMAND_LIMIT) }\n }\n return {}\n}\n\n// Node names must be strings: any other shape (number/object from a hostile or\n// drifted payload) would violate the node event contract (node: string) \u2014 skip\n// silently, mirroring the opencode plugin's resolveNode.\nconst asNode = (v) => (typeof v === 'string' && v.length > 0 ? v : null)\n\nconst writeEvent = async (sessionId, event) => {\n // NOTE: assumes claude is launched at the project root; a subdirectory launch\n // writes to <subdir>/.caesarflow/sessions. Plan 3's `recastflow hook` will\n // resolve the root by walking up for .caesarflow/ instead.\n const dir = join(process.cwd(), '.caesarflow', 'sessions')\n await mkdir(dir, { recursive: true })\n // session_id is host-generated (UUID in practice); sanitize is defense-in-depth\n // and kept identical to the opencode side (writeRecord) so both adapters emit\n // the same filename alphabet for one session.\n const safe = sessionId.replace(/[^A-Za-z0-9_-]/g, '_')\n await appendFile(join(dir, safe + '.jsonl'), JSON.stringify(event) + '\\n')\n}\n\nlet raw = ''\nprocess.stdin.on('data', (d) => { raw += d })\nprocess.stdin.on('end', async () => {\n try {\n const input = JSON.parse(raw)\n const sessionId = input.session_id\n if (!sessionId) process.exit(0)\n const eventName = input.hook_event_name\n const tool = input.tool_name\n let kind = null\n let node = null\n let toolEvent = null\n if (eventName === 'SubagentStart') {\n kind = 'node_start'\n node = asNode(input.agent_type)\n } else if (eventName === 'SubagentStop') {\n kind = 'node_done'\n node = asNode(input.agent_type)\n } else if (eventName === 'PreToolUse' && (tool === 'Task' || tool === 'Agent')) {\n // NOTE: modern CLIs also fire SubagentStart for the same spawn \u2014 duplicate node_start events are expected; core's applyEvent reducer is last-event-wins.\n // NOTE: on legacy CLIs without SubagentStop, node_done is never recorded (sessions appear in-progress); Plan 3 supersedes.\n // NOTE: spawn tools stop here \u2014 no tool_start for Task/Agent (see header).\n kind = 'node_start'\n node = asNode(input.tool_input ? input.tool_input.subagent_type : null)\n } else if (eventName === 'PreToolUse' && typeof tool === 'string' && tool.length > 0) {\n toolEvent = { ts: Date.now(), event: 'tool_start', tool: tool, ...metadataOf(tool, input.tool_input) }\n } else if (eventName === 'PostToolUse' && tool !== 'Task' && tool !== 'Agent' && typeof tool === 'string' && tool.length > 0) {\n // tool name only: no ok/fail is derivable from the PostToolUse payload (see header).\n toolEvent = { ts: Date.now(), event: 'tool_done', tool: tool }\n }\n if (toolEvent) {\n await writeEvent(sessionId, toolEvent)\n process.exit(0)\n }\n if (!kind || !node) process.exit(0)\n await writeEvent(sessionId, { ts: Date.now(), event: kind, node: node })\n } catch {\n process.exit(0) // hooks must never break the CLI\n }\n})\n";
|
|
36
|
+
/**
|
|
37
|
+
* settings.json 产物路径常量:claude-code 的 hooks 注册文件。compile 产出
|
|
38
|
+
* "recastflow 全量 hooks 段"内容,deploy 时经 {@link mergeClaudeSettings} 与
|
|
39
|
+
* 用户已有文件合并(见 flow.ts 的合并写说明)。
|
|
40
|
+
*/
|
|
41
|
+
export declare const CLAUDE_SETTINGS_PATH = ".claude/settings.json";
|
|
42
|
+
/**
|
|
43
|
+
* 合并写 settings.json(Plan 6 锁定决策,取代 v1 的整体覆盖):
|
|
44
|
+
*
|
|
45
|
+
* - `existingText` 为磁盘现状(null = 文件不存在);缺失、坏 JSON、非对象
|
|
46
|
+
* JSON(如手滑写成 `[]`)一律视为空对象——deploy 绝不被用户的手改文件
|
|
47
|
+
* 阻塞,且坏文件被 v2 结构替换后自动"修复"。
|
|
48
|
+
* - 保留全部既有顶层键(model/permissions 等)与用户自己的 hooks 条目;
|
|
49
|
+
* - 只更新 recastflow 拥有的条目:先按 {@link HOOK_COMMAND_MARKER} 从既有
|
|
50
|
+
* hooks 里剔除本工具旧条目(替换语义、幂等的关键),再追加 compile 产出
|
|
51
|
+
* 的新条目。识别方式选定 hook command 里的产物脚本名 `caesarflow-report.mjs`
|
|
52
|
+
* ——matcher/事件名都可能被用户仿写,宽泛子串(如 `recastflow`)还会误伤
|
|
53
|
+
* 路径恰含该字样的用户条目,产物文件名是本工具独有的强指纹。
|
|
54
|
+
* - 幂等:对合并结果重跑同一合并输入,输出逐字节相同(旧条目被剔除后追加
|
|
55
|
+
* 相同内容)。
|
|
56
|
+
*
|
|
57
|
+
* 纯函数(无 IO):读磁盘发生在 flow.ts 的部署编排层,本函数只做文本进文本出。
|
|
58
|
+
*/
|
|
59
|
+
export declare function mergeClaudeSettings(existingText: string | null, compiledText: string): string;
|
|
60
|
+
/**
|
|
61
|
+
* observe 与 deploy 共用的 claude-code「安装段」产物(Plan 6「安装即监控」):
|
|
62
|
+
* hook 上报脚本 + settings.json 的 recastflow hooks 段。单一来源的理由:
|
|
63
|
+
* observe 安装的就是 deploy 会装的钩子,两处各写一份产物清单必然漂移
|
|
64
|
+
* (改脚本忘同步另一侧)——compile 与 observe 安装都从本函数取件,
|
|
65
|
+
* 「产物形状 = deploy 安装产物形状」由测试钉死。
|
|
66
|
+
*
|
|
67
|
+
* settings.json 这里产出的是 recastflow 全量 hooks 段(PreToolUse 不带
|
|
68
|
+
* matcher = 拦截全量工具;PostToolUse 为 v2 新增的 tool_done 通道);
|
|
69
|
+
* **合并写**(保留用户既有键与 hooks)发生在编排层:deploy 走
|
|
70
|
+
* flow.ts 的 withSettingsMerged,observe 安装同径——compile 保持纯函数,
|
|
71
|
+
* 不读磁盘。
|
|
72
|
+
*/
|
|
73
|
+
export declare function observerArtifacts(): CompiledFile[];
|
|
74
|
+
/**
|
|
75
|
+
* claude-code 适配器实例。detect 探测安装状态的两类信号:项目 marker 目录
|
|
76
|
+
* (`<root>/.claude`)与 PATH 上的 `claude` 二进制(探测逻辑共用 shared 的
|
|
77
|
+
* {@link which} / {@link detectDetails})——任一命中即 installed,details
|
|
78
|
+
* 注明命中的是哪类。compile 按节点顺序产出 agent/skill 文件,随后固定追加
|
|
79
|
+
* 入口 command 与安装段(hook 脚本 + settings.json,见 {@link observerArtifacts})。
|
|
80
|
+
*/
|
|
81
|
+
export declare const claudeCodeAdapter: CliAdapter;
|