@abhishek944/pi-firstmate 0.0.0-stage → 0.1.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 +187 -2
- package/config.example.json +6 -0
- package/dist/config.d.ts +32 -0
- package/dist/config.js +97 -0
- package/dist/herdr-banner.d.ts +42 -0
- package/dist/herdr-banner.js +265 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +779 -0
- package/dist/skill-allowlist.d.ts +25 -0
- package/dist/skill-allowlist.js +34 -0
- package/dist/tool-gate.d.ts +30 -0
- package/dist/tool-gate.js +108 -0
- package/dist/worker.d.ts +34 -0
- package/dist/worker.js +82 -0
- package/herdr/firstmate-banner/README.md +5 -0
- package/herdr/firstmate-banner/main.js +161 -0
- package/herdr/firstmate-banner/mod.json +1 -0
- package/package.json +64 -4
- package/skills/close/SKILL.md +61 -0
- package/skills/firstmate/SKILL.md +133 -0
- package/src/config.ts +118 -0
- package/src/herdr-banner.ts +269 -0
- package/src/index.ts +886 -0
- package/src/skill-allowlist.ts +50 -0
- package/src/tool-gate.ts +128 -0
- package/src/worker.ts +101 -0
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional native Herdr banner for First Mate.
|
|
3
|
+
*
|
|
4
|
+
* A personal Herdr layout gets one extra block above the block that contains
|
|
5
|
+
* the shown tab. That block loads `mods/firstmate-banner`, which reads the
|
|
6
|
+
* small per-session state files written here and shows the strip only for the
|
|
7
|
+
* active tab's live First Mate session.
|
|
8
|
+
*
|
|
9
|
+
* Everything in this module is best-effort. First Mate's mode, guidance and
|
|
10
|
+
* status keep working when Herdr is absent, the shown layout is shipped, or a
|
|
11
|
+
* file cannot be written.
|
|
12
|
+
*/
|
|
13
|
+
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
14
|
+
import { homedir } from "node:os";
|
|
15
|
+
import { isAbsolute, join, resolve } from "node:path";
|
|
16
|
+
export const BANNER_MOD = "firstmate-banner";
|
|
17
|
+
export const BANNER_NODE_ID = "firstmate-banner";
|
|
18
|
+
const SHIPPED_LAYOUTS = new Set(["main", "terminals", "win98"]);
|
|
19
|
+
const SESSION_ID = /^[A-Za-z0-9][A-Za-z0-9_-]*$/;
|
|
20
|
+
const TERMINAL_ID = /^(?:[A-Za-z0-9][A-Za-z0-9._-]*\/)?t[0-9a-z]+$/;
|
|
21
|
+
const LAYOUT_NAME = /^[A-Za-z0-9][A-Za-z0-9._-]*$/;
|
|
22
|
+
const ASSET_NAMES = ["main.js", "mod.json", "README.md"];
|
|
23
|
+
function isRecord(value) {
|
|
24
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
function errorText(error) {
|
|
27
|
+
const line = (error instanceof Error ? error.message : String(error)).split("\n", 1)[0]?.trim();
|
|
28
|
+
return line === undefined || line === "" ? "unknown error" : line;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The Herdr app home, matching the app's own lookup: `HERDR_APP_HOME`, else
|
|
32
|
+
* `XDG_CONFIG_HOME/herdr-app`, else `~/.config/herdr-app`.
|
|
33
|
+
*/
|
|
34
|
+
export function herdrAppHome(env = process.env) {
|
|
35
|
+
const custom = env.HERDR_APP_HOME?.trim();
|
|
36
|
+
if (custom !== undefined && custom !== "")
|
|
37
|
+
return resolve(custom);
|
|
38
|
+
const config = env.XDG_CONFIG_HOME?.trim();
|
|
39
|
+
const base = config !== undefined && config !== "" && isAbsolute(config) ? config : join(homedir(), ".config");
|
|
40
|
+
return join(base, "herdr-app");
|
|
41
|
+
}
|
|
42
|
+
function bannerDataDir(appHome) {
|
|
43
|
+
return join(appHome, "data", BANNER_MOD);
|
|
44
|
+
}
|
|
45
|
+
function writeAtomic(path, data) {
|
|
46
|
+
const temporary = `${path}.firstmate-${process.pid}-${Date.now()}.tmp`;
|
|
47
|
+
writeFileSync(temporary, data);
|
|
48
|
+
try {
|
|
49
|
+
renameSync(temporary, path);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
rmSync(temporary, { force: true });
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Keep the previous installed copy next to the recovery notes, not in the live mod folder. */
|
|
57
|
+
function backup(path, name, backupDir) {
|
|
58
|
+
mkdirSync(backupDir, { recursive: true });
|
|
59
|
+
copyFileSync(path, join(backupDir, `${Date.now()}-${name}`));
|
|
60
|
+
}
|
|
61
|
+
/** Install or update only the files this package owns; never duplicate identical content. */
|
|
62
|
+
function installAssets(appHome, assets, backupDir) {
|
|
63
|
+
const directory = join(appHome, "mods", BANNER_MOD);
|
|
64
|
+
mkdirSync(directory, { recursive: true });
|
|
65
|
+
for (const [name, data] of assets) {
|
|
66
|
+
const target = join(directory, name);
|
|
67
|
+
if (existsSync(target) && readFileSync(target).equals(data))
|
|
68
|
+
continue;
|
|
69
|
+
if (existsSync(target))
|
|
70
|
+
backup(target, name, backupDir);
|
|
71
|
+
writeAtomic(target, data);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** The layout the running window shows, from the app's shared state. */
|
|
75
|
+
function shownLayout(appHome) {
|
|
76
|
+
try {
|
|
77
|
+
const parsed = JSON.parse(readFileSync(join(appHome, "shared.json"), "utf8"));
|
|
78
|
+
if (!isRecord(parsed) || typeof parsed.layout !== "string" || !LAYOUT_NAME.test(parsed.layout))
|
|
79
|
+
return undefined;
|
|
80
|
+
return parsed.layout;
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function findNode(node, id) {
|
|
87
|
+
if (!isRecord(node))
|
|
88
|
+
return undefined;
|
|
89
|
+
if (node.id === id)
|
|
90
|
+
return node;
|
|
91
|
+
for (const child of Array.isArray(node.children) ? node.children : []) {
|
|
92
|
+
const found = findNode(child, id);
|
|
93
|
+
if (found !== undefined)
|
|
94
|
+
return found;
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
/** The root-to-block path of the layout node that shows the active tab. */
|
|
99
|
+
function pathToActiveTab(node, trail = []) {
|
|
100
|
+
if (!isRecord(node))
|
|
101
|
+
return undefined;
|
|
102
|
+
const path = [...trail, node];
|
|
103
|
+
if (isRecord(node.content) && node.content.tab === "active")
|
|
104
|
+
return path;
|
|
105
|
+
for (const child of Array.isArray(node.children) ? node.children : []) {
|
|
106
|
+
const found = pathToActiveTab(child, path);
|
|
107
|
+
if (found !== undefined)
|
|
108
|
+
return found;
|
|
109
|
+
}
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Insert directly before the child of the nearest `column` ancestor that
|
|
114
|
+
* contains the active tab. In the shipped main layout that is `work.children`
|
|
115
|
+
* before `work-body`; the tab block, pane frame and side panels are untouched.
|
|
116
|
+
*/
|
|
117
|
+
function insertionSpot(root) {
|
|
118
|
+
const path = pathToActiveTab(root);
|
|
119
|
+
if (path === undefined || path.length < 2)
|
|
120
|
+
return undefined;
|
|
121
|
+
for (let index = path.length - 1; index >= 1; index -= 1) {
|
|
122
|
+
const parent = path[index - 1];
|
|
123
|
+
const child = path[index];
|
|
124
|
+
if (parent === undefined || child === undefined || parent.type !== "column" || !Array.isArray(parent.children))
|
|
125
|
+
continue;
|
|
126
|
+
const at = parent.children.indexOf(child);
|
|
127
|
+
if (at >= 0)
|
|
128
|
+
return { children: parent.children, index: at };
|
|
129
|
+
}
|
|
130
|
+
return undefined;
|
|
131
|
+
}
|
|
132
|
+
function installLayoutNode(appHome, layoutName, backupDir) {
|
|
133
|
+
const path = join(appHome, "layouts", `${layoutName}.json`);
|
|
134
|
+
const original = readFileSync(path, "utf8");
|
|
135
|
+
const tree = JSON.parse(original);
|
|
136
|
+
if (!isRecord(tree))
|
|
137
|
+
throw new Error(`layout "${layoutName}" is not a layout object`);
|
|
138
|
+
if (findNode(tree, BANNER_NODE_ID) !== undefined)
|
|
139
|
+
return "present";
|
|
140
|
+
const spot = insertionSpot(tree);
|
|
141
|
+
if (spot === undefined) {
|
|
142
|
+
throw new Error(`layout "${layoutName}" has no column above its active-tab block, so there is nowhere to place the banner`);
|
|
143
|
+
}
|
|
144
|
+
spot.children.splice(spot.index, 0, {
|
|
145
|
+
id: BANNER_NODE_ID,
|
|
146
|
+
type: "block",
|
|
147
|
+
size: { px: 0 },
|
|
148
|
+
content: { mod: BANNER_MOD },
|
|
149
|
+
});
|
|
150
|
+
backup(path, `${layoutName}.json`, backupDir);
|
|
151
|
+
writeAtomic(path, `${JSON.stringify(tree, null, 2)}\n`);
|
|
152
|
+
return "installed";
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Make the installed mod and the shown personal layout ready for the banner.
|
|
156
|
+
* Callers treat `unavailable` as a diagnostic, never as a First Mate failure.
|
|
157
|
+
*/
|
|
158
|
+
export function ensureHerdrBanner(appHome, assets) {
|
|
159
|
+
try {
|
|
160
|
+
if (!existsSync(appHome) || !statSync(appHome).isDirectory()) {
|
|
161
|
+
return { state: "unavailable", detail: `the Herdr app home ${appHome} does not exist` };
|
|
162
|
+
}
|
|
163
|
+
const contents = new Map();
|
|
164
|
+
for (const name of ASSET_NAMES) {
|
|
165
|
+
try {
|
|
166
|
+
contents.set(name, readFileSync(new URL(name, assets)));
|
|
167
|
+
}
|
|
168
|
+
catch {
|
|
169
|
+
return { state: "unavailable", detail: `the packaged banner asset ${name} is missing` };
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const backupDir = join(bannerDataDir(appHome), "backups");
|
|
173
|
+
const modDir = join(appHome, "mods", BANNER_MOD);
|
|
174
|
+
const hadMod = existsSync(join(modDir, "main.js"));
|
|
175
|
+
installAssets(appHome, contents, backupDir);
|
|
176
|
+
const layoutName = shownLayout(appHome);
|
|
177
|
+
if (layoutName === undefined) {
|
|
178
|
+
return { state: "unavailable", detail: "the Herdr window has no shown layout yet" };
|
|
179
|
+
}
|
|
180
|
+
if (SHIPPED_LAYOUTS.has(layoutName)) {
|
|
181
|
+
return {
|
|
182
|
+
state: "unavailable",
|
|
183
|
+
detail: `the shown layout "${layoutName}" is shipped by Herdr; switch to a personal layout to show the banner`,
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
if (!existsSync(join(appHome, "layouts", `${layoutName}.json`))) {
|
|
187
|
+
return { state: "unavailable", detail: `the shown layout file ${layoutName}.json is missing` };
|
|
188
|
+
}
|
|
189
|
+
const layout = installLayoutNode(appHome, layoutName, backupDir);
|
|
190
|
+
if (layout === "present" && hadMod)
|
|
191
|
+
return { state: "present", detail: `present in Herdr layout "${layoutName}"` };
|
|
192
|
+
return { state: "installed", detail: `installed in Herdr layout "${layoutName}"` };
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
return { state: "unavailable", detail: errorText(error) };
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Publish this session's effective mode for the banner mod. The file is
|
|
200
|
+
* replaced atomically; a file left by an earlier Pi session in the same
|
|
201
|
+
* terminal is removed so it cannot override this one.
|
|
202
|
+
*/
|
|
203
|
+
export function writeBannerState(appHome, state) {
|
|
204
|
+
if (!SESSION_ID.test(state.sessionId))
|
|
205
|
+
throw new Error("invalid Pi session ID");
|
|
206
|
+
if (!TERMINAL_ID.test(state.terminal))
|
|
207
|
+
throw new Error("invalid Core terminal address");
|
|
208
|
+
const directory = bannerDataDir(appHome);
|
|
209
|
+
mkdirSync(directory, { recursive: true });
|
|
210
|
+
for (const name of readdirSync(directory)) {
|
|
211
|
+
if (!name.endsWith(".json"))
|
|
212
|
+
continue;
|
|
213
|
+
const path = join(directory, name);
|
|
214
|
+
try {
|
|
215
|
+
const value = JSON.parse(readFileSync(path, "utf8"));
|
|
216
|
+
if (isRecord(value) && value.terminal === state.terminal && value.sessionId !== state.sessionId)
|
|
217
|
+
rmSync(path, { force: true });
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
// A damaged foreign file is ignored; it can never match the banner.
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
writeAtomic(join(directory, `${state.sessionId}.json`), `${JSON.stringify({
|
|
224
|
+
version: 1,
|
|
225
|
+
sessionId: state.sessionId,
|
|
226
|
+
terminal: state.terminal,
|
|
227
|
+
effective: state.effective,
|
|
228
|
+
updatedAt: Date.now(),
|
|
229
|
+
})}\n`);
|
|
230
|
+
}
|
|
231
|
+
/** Best-effort shutdown cleanup; the banner disappears as soon as this file is gone. */
|
|
232
|
+
export function clearBannerState(appHome, sessionId, terminal) {
|
|
233
|
+
if (!SESSION_ID.test(sessionId))
|
|
234
|
+
return;
|
|
235
|
+
const directory = bannerDataDir(appHome);
|
|
236
|
+
try {
|
|
237
|
+
rmSync(join(directory, `${sessionId}.json`), { force: true });
|
|
238
|
+
}
|
|
239
|
+
catch {
|
|
240
|
+
// Nothing useful to do on shutdown.
|
|
241
|
+
}
|
|
242
|
+
if (terminal === undefined || !TERMINAL_ID.test(terminal))
|
|
243
|
+
return;
|
|
244
|
+
// A primary session that crashed leaves a state file behind. If this
|
|
245
|
+
// terminal now runs a session that must not advertise (for example a
|
|
246
|
+
// worker), remove that leftover too so it cannot win by being older.
|
|
247
|
+
try {
|
|
248
|
+
for (const name of readdirSync(directory)) {
|
|
249
|
+
if (!name.endsWith(".json"))
|
|
250
|
+
continue;
|
|
251
|
+
const path = join(directory, name);
|
|
252
|
+
try {
|
|
253
|
+
const value = JSON.parse(readFileSync(path, "utf8"));
|
|
254
|
+
if (isRecord(value) && value.terminal === terminal && value.sessionId !== sessionId)
|
|
255
|
+
rmSync(path, { force: true });
|
|
256
|
+
}
|
|
257
|
+
catch {
|
|
258
|
+
// Ignore a damaged foreign file.
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
catch {
|
|
263
|
+
// The directory may not exist yet.
|
|
264
|
+
}
|
|
265
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* First Mate - configurable coordinator mode for primary Pi sessions.
|
|
3
|
+
*
|
|
4
|
+
* The mode keeps conversation, decisions and quick inspection in the main
|
|
5
|
+
* session, and delegates project execution to workers through an available
|
|
6
|
+
* installed orchestration backend. The behavioral policy lives in
|
|
7
|
+
* `skills/firstmate/SKILL.md`; this extension only owns configuration, the
|
|
8
|
+
* session toggle, worker suppression, status and per-run prompt guidance.
|
|
9
|
+
*/
|
|
10
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
11
|
+
export default function firstMateExtension(pi: ExtensionAPI): void;
|