@meshpoint/cli 0.0.0-stage → 1.0.0-rc.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 +202 -0
- package/README.md +122 -2
- package/dist/antigravity/connect.js +50 -0
- package/dist/antigravity/tap.js +123 -0
- package/dist/antigravity/transcript.js +88 -0
- package/dist/bin.js +327 -0
- package/dist/checkpoints.js +58 -0
- package/dist/claude-code/connect.js +35 -0
- package/dist/claude-code/settings.js +45 -0
- package/dist/claude-code/skill.js +56 -0
- package/dist/claude-code/tap.js +58 -0
- package/dist/claude-code/transcript.js +75 -0
- package/dist/codex/connect.js +67 -0
- package/dist/codex/transcript.js +59 -0
- package/dist/config.js +66 -0
- package/dist/control.js +179 -0
- package/dist/cursor/connect.js +64 -0
- package/dist/cursor/tap.js +68 -0
- package/dist/cursor/transcript.js +74 -0
- package/dist/detect.js +130 -0
- package/dist/doctor.js +101 -0
- package/dist/items.js +91 -0
- package/dist/openclaw/connect.js +45 -0
- package/dist/openclaw/pack.js +153 -0
- package/dist/openclaw/tap.js +124 -0
- package/dist/opencode/connect.js +40 -0
- package/dist/opencode/hooks.js +176 -0
- package/dist/opencode/plugin.js +5 -0
- package/dist/pi/connect.js +50 -0
- package/dist/pi/extension.js +118 -0
- package/dist/runtime.js +26 -0
- package/dist/spool.js +79 -0
- package/dist/tap.js +167 -0
- package/package.json +46 -4
package/dist/detect.js
ADDED
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
// Finds the coding harnesses present on this machine and whether each already runs the meshpoint
|
|
2
|
+
// tap. `meshpoint connect` with no name prints this; `meshpoint doctor` reuses it. Nothing here
|
|
3
|
+
// writes anything.
|
|
4
|
+
import { existsSync, lstatSync, readFileSync, statSync } from "node:fs";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { antigravityPaths, MESHPOINT_GROUP } from "./antigravity/connect.js";
|
|
8
|
+
import { claudeConfigDir } from "./claude-code/connect.js";
|
|
9
|
+
import { TAP_COMMAND } from "./claude-code/settings.js";
|
|
10
|
+
import { CODEX_TAP_COMMAND, codexPaths } from "./codex/connect.js";
|
|
11
|
+
import { CURSOR_TAP_COMMAND, cursorPaths } from "./cursor/connect.js";
|
|
12
|
+
import { openclawHooksDir } from "./openclaw/connect.js";
|
|
13
|
+
import { PACK_NAME } from "./openclaw/pack.js";
|
|
14
|
+
import { opencodeConfigDir, opencodePaths } from "./opencode/connect.js";
|
|
15
|
+
import { piPaths } from "./pi/connect.js";
|
|
16
|
+
export const HARNESSES = ["claude-code", "codex", "cursor", "antigravity", "pi", "opencode", "openclaw"];
|
|
17
|
+
function onPath(bin, env) {
|
|
18
|
+
for (const dir of (env.PATH ?? "").split(path.delimiter)) {
|
|
19
|
+
if (!dir)
|
|
20
|
+
continue;
|
|
21
|
+
const candidate = path.join(dir, bin);
|
|
22
|
+
try {
|
|
23
|
+
if (statSync(candidate).isFile())
|
|
24
|
+
return candidate;
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
function found(dir, bin, env) {
|
|
33
|
+
if (existsSync(dir))
|
|
34
|
+
return dir;
|
|
35
|
+
return onPath(bin, env);
|
|
36
|
+
}
|
|
37
|
+
function fileHas(file, needle) {
|
|
38
|
+
try {
|
|
39
|
+
return readFileSync(file, "utf8").includes(needle);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function isLink(file) {
|
|
46
|
+
try {
|
|
47
|
+
return lstatSync(file).isSymbolicLink();
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return false;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export function detectHarnesses(opts = {}) {
|
|
54
|
+
const env = opts.env ?? process.env;
|
|
55
|
+
const home = opts.home ?? homedir();
|
|
56
|
+
const project = opts.projectDir;
|
|
57
|
+
const scope = project ? ` --project ${project}` : "";
|
|
58
|
+
const claudeDir = project ? path.join(project, ".claude") : claudeConfigDir(env, home);
|
|
59
|
+
const codex = codexPaths(project, env, home);
|
|
60
|
+
const cursor = cursorPaths(project, home);
|
|
61
|
+
const antigravity = antigravityPaths(project, home);
|
|
62
|
+
const pi = piPaths(undefined, env, home);
|
|
63
|
+
const opencode = opencodePaths(opencodeConfigDir(env, home));
|
|
64
|
+
const openclawPack = path.join(openclawHooksDir(env, home), PACK_NAME);
|
|
65
|
+
const claudeSettings = path.join(claudeDir, "settings.json");
|
|
66
|
+
return [
|
|
67
|
+
{
|
|
68
|
+
harness: "claude-code",
|
|
69
|
+
detected: found(claudeConfigDir(env, home), "claude", env),
|
|
70
|
+
hookPath: claudeSettings,
|
|
71
|
+
connected: fileHas(claudeSettings, TAP_COMMAND),
|
|
72
|
+
command: `meshpoint connect claude-code${scope}`,
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
harness: "codex",
|
|
76
|
+
detected: found(env.CODEX_HOME || path.join(home, ".codex"), "codex", env),
|
|
77
|
+
hookPath: codex.hooks,
|
|
78
|
+
connected: fileHas(codex.hooks, CODEX_TAP_COMMAND),
|
|
79
|
+
command: `meshpoint connect codex${scope}`,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
harness: "cursor",
|
|
83
|
+
detected: found(path.join(home, ".cursor"), "cursor", env),
|
|
84
|
+
hookPath: cursor.hooks,
|
|
85
|
+
connected: fileHas(cursor.hooks, CURSOR_TAP_COMMAND),
|
|
86
|
+
command: `meshpoint connect cursor${scope}`,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
harness: "antigravity",
|
|
90
|
+
detected: found(path.join(home, ".gemini"), "antigravity", env),
|
|
91
|
+
hookPath: antigravity.hooks,
|
|
92
|
+
connected: fileHas(antigravity.hooks, `"${MESHPOINT_GROUP}"`),
|
|
93
|
+
command: `meshpoint connect antigravity${scope}`,
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
harness: "pi",
|
|
97
|
+
detected: found(path.dirname(path.dirname(pi.extension)), "pi", env),
|
|
98
|
+
hookPath: pi.extension,
|
|
99
|
+
connected: fileHas(pi.extension, "meshpoint connect pi"),
|
|
100
|
+
command: "meshpoint connect pi",
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
harness: "opencode",
|
|
104
|
+
detected: found(opencodeConfigDir(env, home), "opencode", env),
|
|
105
|
+
hookPath: opencode.plugin,
|
|
106
|
+
connected: isLink(opencode.plugin),
|
|
107
|
+
command: "meshpoint connect opencode",
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
harness: "openclaw",
|
|
111
|
+
detected: found(path.dirname(openclawHooksDir(env, home)), "openclaw", env),
|
|
112
|
+
hookPath: openclawPack,
|
|
113
|
+
connected: existsSync(openclawPack),
|
|
114
|
+
command: "meshpoint connect openclaw",
|
|
115
|
+
},
|
|
116
|
+
];
|
|
117
|
+
}
|
|
118
|
+
// The table `meshpoint connect` prints. Agents read it; keep the columns stable.
|
|
119
|
+
export function renderDetected(rows) {
|
|
120
|
+
const lines = ["harness detected connected command"];
|
|
121
|
+
for (const r of rows) {
|
|
122
|
+
lines.push(`${r.harness.padEnd(12)} ${(r.detected ? "yes" : "no").padEnd(9)} ${(r.connected ? "yes" : "no").padEnd(10)} ${r.command}`);
|
|
123
|
+
}
|
|
124
|
+
const present = rows.filter((r) => r.detected);
|
|
125
|
+
lines.push("");
|
|
126
|
+
lines.push(present.length === 0
|
|
127
|
+
? "No harness found. Install one, or run the connect command for the harness you use anyway."
|
|
128
|
+
: `Found ${present.length}: ${present.map((r) => r.harness).join(", ")}. Run the connect command for each one you want on the mesh.`);
|
|
129
|
+
return lines.join("\n");
|
|
130
|
+
}
|
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
// `meshpoint doctor`: is this machine ready for the mesh, and is this repo on it? Each check is
|
|
2
|
+
// ok, fail or info; the command exits 1 only on a fail. Agents run this after connecting.
|
|
3
|
+
import { accessSync, constants, mkdirSync, readdirSync } from "node:fs";
|
|
4
|
+
import { homedir } from "node:os";
|
|
5
|
+
import { configPath, loadConfig, repoContext, spoolDir } from "./config.js";
|
|
6
|
+
import { api, ApiError, freshConfig } from "./control.js";
|
|
7
|
+
import { detectHarnesses } from "./detect.js";
|
|
8
|
+
export function agoText(at, now) {
|
|
9
|
+
const s = Math.max(0, Math.round((now - at) / 1000));
|
|
10
|
+
if (s < 60)
|
|
11
|
+
return `${s}s ago`;
|
|
12
|
+
const m = Math.round(s / 60);
|
|
13
|
+
if (m < 60)
|
|
14
|
+
return `${m} min ago`;
|
|
15
|
+
const h = Math.round(m / 60);
|
|
16
|
+
if (h < 48)
|
|
17
|
+
return `${h} h ago`;
|
|
18
|
+
return `${Math.round(h / 24)} d ago`;
|
|
19
|
+
}
|
|
20
|
+
async function account(config, fetchImpl, env, home) {
|
|
21
|
+
const call = (c) => api(c.baseUrl, "GET", "/v1/me", { token: c.token, fetch: fetchImpl });
|
|
22
|
+
try {
|
|
23
|
+
return { me: await call(config), config };
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
if (!(err instanceof ApiError) || err.status !== 401 || !config.refresh)
|
|
27
|
+
throw err;
|
|
28
|
+
const next = await freshConfig(config, { force: true, ...(fetchImpl ? { fetch: fetchImpl } : {}), env, home });
|
|
29
|
+
return { me: await call(next), config: next };
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export async function runDoctor(opts = {}) {
|
|
33
|
+
const env = opts.env ?? process.env;
|
|
34
|
+
const home = opts.home ?? homedir();
|
|
35
|
+
const cwd = opts.cwd ?? process.cwd();
|
|
36
|
+
const now = opts.now ?? Date.now();
|
|
37
|
+
const checks = [];
|
|
38
|
+
let config = loadConfig(env, home);
|
|
39
|
+
if (config)
|
|
40
|
+
checks.push({ id: "config", status: "ok", detail: `${configPath(env, home)} (${config.baseUrl})` });
|
|
41
|
+
else
|
|
42
|
+
checks.push({ id: "config", status: "fail", detail: `no login at ${configPath(env, home)}; run meshpoint login` });
|
|
43
|
+
if (config) {
|
|
44
|
+
try {
|
|
45
|
+
const got = await account(config, opts.fetch, env, home);
|
|
46
|
+
config = got.config;
|
|
47
|
+
checks.push({ id: "token", status: "ok", detail: `${got.me.user.email}, org ${got.me.org?.name ?? "none"}${got.me.org ? ` (${got.me.org.role})` : ""}` });
|
|
48
|
+
}
|
|
49
|
+
catch (err) {
|
|
50
|
+
checks.push({ id: "token", status: "fail", detail: `token rejected: ${err instanceof Error ? err.message : String(err)}; run meshpoint login` });
|
|
51
|
+
config = null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const repo = repoContext(cwd);
|
|
55
|
+
if (repo)
|
|
56
|
+
checks.push({ id: "repo", status: "ok", detail: `${repo.project} on ${repo.branch}` });
|
|
57
|
+
else
|
|
58
|
+
checks.push({ id: "repo", status: "fail", detail: `${cwd} is not inside a git repo with an origin remote` });
|
|
59
|
+
const userScope = detectHarnesses({ env, home });
|
|
60
|
+
const projectScope = repo ? detectHarnesses({ env, home, projectDir: repo.root }) : [];
|
|
61
|
+
const present = userScope.filter((h) => h.detected);
|
|
62
|
+
if (present.length === 0)
|
|
63
|
+
checks.push({ id: "harness", status: "fail", detail: "no coding harness found on this machine" });
|
|
64
|
+
for (const h of present) {
|
|
65
|
+
const inProject = projectScope.find((p) => p.harness === h.harness);
|
|
66
|
+
const where = h.connected ? "user scope" : inProject?.connected ? `this repo (${inProject.hookPath})` : null;
|
|
67
|
+
checks.push(where ? { id: `harness:${h.harness}`, status: "ok", detail: `connected in ${where}` } : { id: `harness:${h.harness}`, status: "fail", detail: `found but not connected; run ${h.command}` });
|
|
68
|
+
}
|
|
69
|
+
if (config && repo) {
|
|
70
|
+
try {
|
|
71
|
+
const summary = await api(config.baseUrl, "GET", `/v1/project?project=${encodeURIComponent(repo.project)}`, { token: config.token, fetch: opts.fetch });
|
|
72
|
+
if (summary.last_capture_at)
|
|
73
|
+
checks.push({ id: "capture", status: "ok", detail: `last capture ${agoText(summary.last_capture_at, now)}, ${summary.items} items` });
|
|
74
|
+
else
|
|
75
|
+
checks.push({ id: "capture", status: "info", detail: "no capture yet for this project; start a session in a connected harness" });
|
|
76
|
+
}
|
|
77
|
+
catch (err) {
|
|
78
|
+
if (err instanceof ApiError && err.status === 404)
|
|
79
|
+
checks.push({ id: "capture", status: "info", detail: "project not on the platform yet; the first session creates it" });
|
|
80
|
+
else
|
|
81
|
+
checks.push({ id: "capture", status: "fail", detail: `could not read the project: ${err instanceof Error ? err.message : String(err)}` });
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
const spool = spoolDir(env, home);
|
|
85
|
+
try {
|
|
86
|
+
mkdirSync(spool, { recursive: true, mode: 0o700 });
|
|
87
|
+
accessSync(spool, constants.W_OK);
|
|
88
|
+
const pending = readdirSync(spool).filter((f) => f.endsWith(".jsonl")).length;
|
|
89
|
+
checks.push({ id: "spool", status: "ok", detail: `${spool} writable, ${pending} pending file${pending === 1 ? "" : "s"}` });
|
|
90
|
+
}
|
|
91
|
+
catch (err) {
|
|
92
|
+
checks.push({ id: "spool", status: "fail", detail: `${spool} not writable: ${err instanceof Error ? err.message : String(err)}` });
|
|
93
|
+
}
|
|
94
|
+
return { ok: checks.every((c) => c.status !== "fail"), checks };
|
|
95
|
+
}
|
|
96
|
+
export function renderDoctor(report) {
|
|
97
|
+
const mark = { ok: "✓", fail: "✗", info: "·" };
|
|
98
|
+
const lines = report.checks.map((c) => `${mark[c.status]} ${c.id.padEnd(20)} ${c.detail}`);
|
|
99
|
+
lines.push(report.ok ? "ready" : "not ready: fix the ✗ lines above");
|
|
100
|
+
return lines.join("\n");
|
|
101
|
+
}
|
package/dist/items.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { KINDS, MeshpointClient } from "@meshpoint/sdk";
|
|
3
|
+
export const ITEMS_MAX = 60;
|
|
4
|
+
export const ID_PREFIX_CHARS = 8;
|
|
5
|
+
const ALL_ITEMS_BUDGET = 100_000;
|
|
6
|
+
const TITLE_MAX = 200;
|
|
7
|
+
const BODY_MAX = 2000;
|
|
8
|
+
function isRecord(v) {
|
|
9
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
10
|
+
}
|
|
11
|
+
function draft(i, kind, title, body) {
|
|
12
|
+
if (typeof kind !== "string" || !KINDS.includes(kind))
|
|
13
|
+
throw new Error(`item ${i}: kind must be one of ${KINDS.join(", ")}`);
|
|
14
|
+
if (typeof title !== "string" || !title.trim())
|
|
15
|
+
throw new Error(`item ${i}: title required`);
|
|
16
|
+
if (typeof body !== "string" || !body.trim())
|
|
17
|
+
throw new Error(`item ${i}: body required`);
|
|
18
|
+
return { kind: kind, title: title.trim().slice(0, TITLE_MAX), body: body.trim().slice(0, BODY_MAX) };
|
|
19
|
+
}
|
|
20
|
+
// `--item <kind> <title> <body>`, repeated. Plain arguments: no JSON for a shell guard to object to.
|
|
21
|
+
export function parseItemArgs(args) {
|
|
22
|
+
const drafts = [];
|
|
23
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
24
|
+
if (args[i] !== "--item")
|
|
25
|
+
throw new Error(`unexpected argument ${args[i]}`);
|
|
26
|
+
drafts.push(draft(drafts.length, args[i + 1], args[i + 2], args[i + 3]));
|
|
27
|
+
i += 3;
|
|
28
|
+
}
|
|
29
|
+
if (drafts.length === 0)
|
|
30
|
+
throw new Error("no items");
|
|
31
|
+
if (drafts.length > ITEMS_MAX)
|
|
32
|
+
throw new Error(`at most ${ITEMS_MAX} items per put`);
|
|
33
|
+
return drafts;
|
|
34
|
+
}
|
|
35
|
+
// Parses the agent's JSON array of {kind, title, body}; anything else is a usage error.
|
|
36
|
+
export function parseItems(raw) {
|
|
37
|
+
let parsed;
|
|
38
|
+
try {
|
|
39
|
+
parsed = JSON.parse(raw);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
throw new Error("items must be a JSON array of {kind, title, body}");
|
|
43
|
+
}
|
|
44
|
+
if (!Array.isArray(parsed) || parsed.length === 0)
|
|
45
|
+
throw new Error("items must be a non-empty JSON array");
|
|
46
|
+
if (parsed.length > ITEMS_MAX)
|
|
47
|
+
throw new Error(`at most ${ITEMS_MAX} items per put`);
|
|
48
|
+
return parsed.map((v, i) => {
|
|
49
|
+
if (!isRecord(v))
|
|
50
|
+
throw new Error(`item ${i}: not an object`);
|
|
51
|
+
return draft(i, v.kind, v.title, v.body);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function clientFor(deps) {
|
|
55
|
+
return new MeshpointClient({
|
|
56
|
+
baseUrl: deps.config.baseUrl,
|
|
57
|
+
token: deps.config.token,
|
|
58
|
+
project: deps.repo.project,
|
|
59
|
+
branch: deps.repo.branch,
|
|
60
|
+
...(deps.fetch ? { fetch: deps.fetch } : {}),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
export async function putItems(drafts, deps) {
|
|
64
|
+
const now = deps.now ?? Date.now;
|
|
65
|
+
const items = drafts.map((d) => ({ id: randomUUID(), ...d }));
|
|
66
|
+
const out = await clientFor(deps).putItems(items);
|
|
67
|
+
deps.checkpoints.markCheckpoint(deps.repo.project, deps.repo.branch, now());
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
// The inject shows the first ID_PREFIX_CHARS of each id, so a retract takes a prefix and resolves it
|
|
71
|
+
// against the branch's live items; an ambiguous or unknown prefix is an error, never a guess.
|
|
72
|
+
export async function retractItems(prefixes, deps) {
|
|
73
|
+
if (prefixes.length === 0)
|
|
74
|
+
throw new Error("retract needs at least one item id");
|
|
75
|
+
const client = clientFor(deps);
|
|
76
|
+
const { items } = await client.recall({ budget: ALL_ITEMS_BUDGET, fresh: true });
|
|
77
|
+
const ids = prefixes.map((prefix) => {
|
|
78
|
+
const matches = items.filter((i) => i.id.startsWith(prefix)).map((i) => i.id);
|
|
79
|
+
if (matches.length === 0)
|
|
80
|
+
throw new Error(`no item on ${deps.repo.branch} starts with ${prefix}`);
|
|
81
|
+
if (matches.length > 1)
|
|
82
|
+
throw new Error(`${prefix} matches ${matches.length} items; give more characters`);
|
|
83
|
+
return matches[0];
|
|
84
|
+
});
|
|
85
|
+
const retracted = [];
|
|
86
|
+
for (const id of ids) {
|
|
87
|
+
if ((await client.retract(id)).found)
|
|
88
|
+
retracted.push(id);
|
|
89
|
+
}
|
|
90
|
+
return { retracted };
|
|
91
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
3
|
+
import { homedir, tmpdir } from "node:os";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
import { HOOK_NAME, PACK_NAME, writePack } from "./pack.js";
|
|
6
|
+
// OpenClaw copies installed hook packs to <state dir>/hooks/<pack id>.
|
|
7
|
+
export function openclawHooksDir(env = process.env, home = homedir()) {
|
|
8
|
+
return path.join(env.OPENCLAW_STATE_DIR || path.join(home, ".openclaw"), "hooks");
|
|
9
|
+
}
|
|
10
|
+
function openclaw(bin, args) {
|
|
11
|
+
try {
|
|
12
|
+
return execFileSync(bin, args, { stdio: ["ignore", "pipe", "pipe"], encoding: "utf8", timeout: 120_000 });
|
|
13
|
+
}
|
|
14
|
+
catch (err) {
|
|
15
|
+
const e = err;
|
|
16
|
+
const detail = (e.stderr || e.stdout || e.message).trim();
|
|
17
|
+
throw new Error(`${bin} ${args.join(" ")} failed: ${detail}`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
// Install goes through `openclaw plugins install` so OpenClaw records and enables the hook itself.
|
|
21
|
+
// `plugins uninstall` does not know hook packs, so removal disables the hook and deletes the dir.
|
|
22
|
+
export function connectOpenclaw(opts) {
|
|
23
|
+
const bin = opts.openclawBin ?? "openclaw";
|
|
24
|
+
const installed = opts.installedDir ?? path.join(openclawHooksDir(), PACK_NAME);
|
|
25
|
+
if (opts.remove) {
|
|
26
|
+
try {
|
|
27
|
+
openclaw(bin, ["hooks", "disable", HOOK_NAME]);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
// Already disabled or never discovered; the directory is what keeps the hook loadable.
|
|
31
|
+
}
|
|
32
|
+
rmSync(installed, { recursive: true, force: true });
|
|
33
|
+
return `removed hook pack ${PACK_NAME} from ${installed}. Restart the OpenClaw gateway to unload it.`;
|
|
34
|
+
}
|
|
35
|
+
const packDir = opts.packDir ?? mkdtempSync(path.join(tmpdir(), "meshpoint-openclaw-"));
|
|
36
|
+
try {
|
|
37
|
+
writePack(packDir);
|
|
38
|
+
openclaw(bin, ["plugins", "install", packDir, "--force"]);
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
if (!opts.packDir)
|
|
42
|
+
rmSync(packDir, { recursive: true, force: true });
|
|
43
|
+
}
|
|
44
|
+
return `installed hook pack ${PACK_NAME} (hook ${HOOK_NAME}) into ${installed}. The gateway needs the meshpoint CLI on its PATH and a login; set MESHPOINT_PROJECT in its environment when the workspace is not a git checkout. Restart the gateway if hooks do not reload automatically.`;
|
|
45
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
// The hook pack OpenClaw copies into <state dir>/hooks/<PACK_NAME>/. It carries no dependencies:
|
|
4
|
+
// the handler shells out to the meshpoint CLI, which owns login, spooling and the platform calls.
|
|
5
|
+
export const PACK_NAME = "meshpoint-openclaw";
|
|
6
|
+
export const HOOK_NAME = "meshpoint";
|
|
7
|
+
export const OPENCLAW_TAP_COMMAND = "meshpoint tap openclaw";
|
|
8
|
+
// Exact keys only: a family key ("message") next to an exact one would fire the handler twice.
|
|
9
|
+
export const EVENTS = ["agent:bootstrap", "message:received", "message:sent", "command:new", "command:reset", "session:auto-reset"];
|
|
10
|
+
const HOOK_MD = `---
|
|
11
|
+
name: ${HOOK_NAME}
|
|
12
|
+
description: "Meshpoint team memory: recall injected at agent bootstrap, turns captured through the meshpoint CLI"
|
|
13
|
+
homepage: https://meshpoint.dev
|
|
14
|
+
metadata:
|
|
15
|
+
{
|
|
16
|
+
"openclaw":
|
|
17
|
+
{
|
|
18
|
+
"events": ${JSON.stringify(EVENTS)},
|
|
19
|
+
"requires": { "bins": ["meshpoint"] },
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# Meshpoint
|
|
25
|
+
|
|
26
|
+
Hands every event to \`${OPENCLAW_TAP_COMMAND}\` as JSON on stdin. The CLI must be logged in
|
|
27
|
+
(\`meshpoint login\`) on the Gateway host and on the Gateway's PATH.
|
|
28
|
+
|
|
29
|
+
- **agent:bootstrap**: fetches team knowledge, the last checkpoint and active teammates for the
|
|
30
|
+
project, and adds them to the bootstrap context (refreshed every 15 minutes per session).
|
|
31
|
+
- **message:received** / **message:sent**: records the user and assistant turns.
|
|
32
|
+
- **command:new**, **command:reset**, **session:auto-reset**: flushes what is still spooled.
|
|
33
|
+
|
|
34
|
+
The project is the origin remote of the agent workspace when it is a git checkout. For a chat
|
|
35
|
+
agent without a repo, set \`MESHPOINT_PROJECT\` (and optionally \`MESHPOINT_BRANCH\`, default
|
|
36
|
+
\`main\`) in the Gateway's environment. Without either, the hook does nothing.
|
|
37
|
+
|
|
38
|
+
Every call is fail-quiet: a broken or unreachable platform never blocks a reply.
|
|
39
|
+
|
|
40
|
+
Remove with \`meshpoint connect openclaw --remove\`.
|
|
41
|
+
`;
|
|
42
|
+
const HANDLER_JS = `// Meshpoint hook for OpenClaw. Each event is handed to the meshpoint CLI as JSON on stdin; this
|
|
43
|
+
// module only keeps what must live between events: the workspace directory, the context block to
|
|
44
|
+
// inject at bootstrap, and a pending checkpoint nudge. Failures are swallowed so the Gateway is
|
|
45
|
+
// never blocked; set MESHPOINT_HOOK_DEBUG=1 to see them in the Gateway log.
|
|
46
|
+
import { execFile } from "node:child_process";
|
|
47
|
+
|
|
48
|
+
const TIMEOUT_MS = 20_000;
|
|
49
|
+
const INJECT_TTL_MS = 15 * 60_000;
|
|
50
|
+
const MAX_SESSIONS = 256;
|
|
51
|
+
const injects = new Map();
|
|
52
|
+
const nudges = new Map();
|
|
53
|
+
let workspaceDir;
|
|
54
|
+
|
|
55
|
+
const str = (v) => (typeof v === "string" ? v : undefined);
|
|
56
|
+
|
|
57
|
+
function debug(message) {
|
|
58
|
+
if (process.env.MESHPOINT_HOOK_DEBUG) console.warn(\`[meshpoint] \${message}\`);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function remember(map, key, value) {
|
|
62
|
+
map.delete(key);
|
|
63
|
+
map.set(key, value);
|
|
64
|
+
if (map.size > MAX_SESSIONS) map.delete(map.keys().next().value);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Message events carry only the session key and bootstrap carries both, so the nudge is filed
|
|
68
|
+
// under every id the event names and looked up the same way.
|
|
69
|
+
function sessionOf(event) {
|
|
70
|
+
return str(event.sessionKey) || str(event.context?.sessionId) || "unknown";
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function idsOf(event) {
|
|
74
|
+
return [str(event.sessionKey), str(event.context?.sessionId)].filter(Boolean);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Only the fields the tap reads; context.cfg holds the whole Gateway config and must never leave.
|
|
78
|
+
function payload(event) {
|
|
79
|
+
const c = event.context ?? {};
|
|
80
|
+
return {
|
|
81
|
+
type: event.type,
|
|
82
|
+
action: event.action,
|
|
83
|
+
sessionKey: event.sessionKey,
|
|
84
|
+
context: { sessionId: str(c.sessionId), workspaceDir: str(c.workspaceDir) ?? workspaceDir, content: str(c.content), success: c.success },
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function tap(event) {
|
|
89
|
+
return new Promise((resolve) => {
|
|
90
|
+
const child = execFile("meshpoint", ["tap", "openclaw"], { timeout: TIMEOUT_MS, maxBuffer: 1 << 20 }, (err, stdout) => {
|
|
91
|
+
if (err) {
|
|
92
|
+
debug(\`tap failed: \${err.message}\`);
|
|
93
|
+
return resolve({});
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
resolve(JSON.parse(stdout) ?? {});
|
|
97
|
+
} catch {
|
|
98
|
+
resolve({});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
child.stdin.on("error", () => undefined);
|
|
102
|
+
child.stdin.end(JSON.stringify(payload(event)));
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export default async function handler(event) {
|
|
107
|
+
try {
|
|
108
|
+
const key = \`\${event.type}:\${event.action}\`;
|
|
109
|
+
const session = sessionOf(event);
|
|
110
|
+
if (key === "agent:bootstrap") {
|
|
111
|
+
workspaceDir = str(event.context?.workspaceDir) ?? workspaceDir;
|
|
112
|
+
let cached = injects.get(session);
|
|
113
|
+
if (!cached || Date.now() - cached.at > INJECT_TTL_MS) {
|
|
114
|
+
cached = { text: str((await tap(event)).inject) ?? "", at: Date.now() };
|
|
115
|
+
remember(injects, session, cached);
|
|
116
|
+
}
|
|
117
|
+
const nudge = idsOf(event).map((id) => nudges.get(id)).find(Boolean);
|
|
118
|
+
for (const id of idsOf(event)) nudges.delete(id);
|
|
119
|
+
const text = [cached.text, nudge].filter(Boolean).join("\\n\\n");
|
|
120
|
+
if (text && workspaceDir && Array.isArray(event.context.bootstrapFiles)) {
|
|
121
|
+
event.context.bootstrapFiles.push({ name: "MESHPOINT.md", path: \`\${workspaceDir}/MESHPOINT.md\`, content: text, missing: false });
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const out = await tap(event);
|
|
126
|
+
if (key === "message:sent" && str(out.nudge)) for (const id of idsOf(event)) remember(nudges, id, out.nudge);
|
|
127
|
+
if (key === "command:new" || key === "command:reset" || key === "session:auto-reset") {
|
|
128
|
+
for (const id of idsOf(event)) {
|
|
129
|
+
injects.delete(id);
|
|
130
|
+
nudges.delete(id);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
} catch (err) {
|
|
134
|
+
debug(\`handler failed: \${err instanceof Error ? err.message : String(err)}\`);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
`;
|
|
138
|
+
const PACKAGE_JSON = `${JSON.stringify({
|
|
139
|
+
name: PACK_NAME,
|
|
140
|
+
version: "1.0.0",
|
|
141
|
+
private: true,
|
|
142
|
+
description: "Meshpoint team memory hook for OpenClaw",
|
|
143
|
+
type: "module",
|
|
144
|
+
openclaw: { hooks: ["."] },
|
|
145
|
+
}, null, 2)}\n`;
|
|
146
|
+
export function packFiles() {
|
|
147
|
+
return { "HOOK.md": HOOK_MD, "handler.js": HANDLER_JS, "package.json": PACKAGE_JSON };
|
|
148
|
+
}
|
|
149
|
+
export function writePack(dir) {
|
|
150
|
+
mkdirSync(dir, { recursive: true });
|
|
151
|
+
for (const [name, content] of Object.entries(packFiles()))
|
|
152
|
+
writeFileSync(path.join(dir, name), content);
|
|
153
|
+
}
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { Tap } from "../tap.js";
|
|
4
|
+
const PROJECT_RE = /^[A-Za-z0-9._-]{1,128}$/;
|
|
5
|
+
const BRANCH_RE = /^[A-Za-z0-9._/-]{1,128}$/;
|
|
6
|
+
// The workspace's origin remote when it is a git checkout; otherwise MESHPOINT_PROJECT names the
|
|
7
|
+
// project explicitly, since a chat agent has no cwd to derive one from.
|
|
8
|
+
export function openclawRepo(workspaceDir, env, repo) {
|
|
9
|
+
if (workspaceDir) {
|
|
10
|
+
const found = repo(workspaceDir);
|
|
11
|
+
if (found)
|
|
12
|
+
return found;
|
|
13
|
+
}
|
|
14
|
+
const project = env.MESHPOINT_PROJECT;
|
|
15
|
+
if (!project || !PROJECT_RE.test(project))
|
|
16
|
+
return null;
|
|
17
|
+
const branch = env.MESHPOINT_BRANCH || "main";
|
|
18
|
+
if (!BRANCH_RE.test(branch))
|
|
19
|
+
return null;
|
|
20
|
+
return { project, branch, root: workspaceDir ?? process.cwd() };
|
|
21
|
+
}
|
|
22
|
+
// OpenClaw exposes no transcript to hooks, so the tap keeps its own record of assistant replies
|
|
23
|
+
// per session for the checkpoint nudge: one line per message:sent, dropped once a nudge fires or
|
|
24
|
+
// the session is replaced.
|
|
25
|
+
export class ReplyLog {
|
|
26
|
+
dir;
|
|
27
|
+
constructor(dir) {
|
|
28
|
+
this.dir = dir;
|
|
29
|
+
}
|
|
30
|
+
file(session) {
|
|
31
|
+
return path.join(this.dir, "openclaw-replies", `${session.replace(/[^A-Za-z0-9._-]/g, "_")}.jsonl`);
|
|
32
|
+
}
|
|
33
|
+
append(session, at, chars) {
|
|
34
|
+
const file = this.file(session);
|
|
35
|
+
mkdirSync(path.dirname(file), { recursive: true, mode: 0o700 });
|
|
36
|
+
appendFileSync(file, `${JSON.stringify({ at, chars })}\n`, { mode: 0o600 });
|
|
37
|
+
}
|
|
38
|
+
// Replies as transcript entries; the text is padding of the recorded length, which is all the
|
|
39
|
+
// checkpoint counter reads.
|
|
40
|
+
entries(session) {
|
|
41
|
+
let raw;
|
|
42
|
+
try {
|
|
43
|
+
raw = readFileSync(this.file(session), "utf8");
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return [];
|
|
47
|
+
}
|
|
48
|
+
const out = [];
|
|
49
|
+
for (const line of raw.split("\n")) {
|
|
50
|
+
if (!line.trim())
|
|
51
|
+
continue;
|
|
52
|
+
try {
|
|
53
|
+
const { at, chars } = JSON.parse(line);
|
|
54
|
+
out.push({ role: "assistant", text: "".padEnd(chars), paths: [], at });
|
|
55
|
+
}
|
|
56
|
+
catch {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return out;
|
|
61
|
+
}
|
|
62
|
+
clear(session) {
|
|
63
|
+
rmSync(this.file(session), { force: true });
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// One OpenClaw event in, one JSON answer out. The Tap swallows mesh failures; this layer only maps
|
|
67
|
+
// OpenClaw's event contract onto it.
|
|
68
|
+
export async function runOpenclawTap(event, deps) {
|
|
69
|
+
const key = `${event.type ?? ""}:${event.action ?? ""}`;
|
|
70
|
+
const context = event.context ?? {};
|
|
71
|
+
const repo = openclawRepo(context.workspaceDir, deps.env, deps.repo);
|
|
72
|
+
if (!repo)
|
|
73
|
+
return "{}";
|
|
74
|
+
// Every event carries the session key; message events carry no session id, so the key is what
|
|
75
|
+
// ties a reply to the bootstrap that follows it.
|
|
76
|
+
const session = event.sessionKey || context.sessionId || "unknown";
|
|
77
|
+
const now = deps.now ?? Date.now;
|
|
78
|
+
const tap = new Tap({
|
|
79
|
+
config: deps.config,
|
|
80
|
+
repo,
|
|
81
|
+
session,
|
|
82
|
+
spool: deps.spool,
|
|
83
|
+
checkpoints: deps.checkpoints,
|
|
84
|
+
...(deps.now ? { now: deps.now } : {}),
|
|
85
|
+
...(deps.fetch ? { fetch: deps.fetch } : {}),
|
|
86
|
+
...(deps.presenceWaitMs !== undefined ? { presenceWaitMs: deps.presenceWaitMs } : {}),
|
|
87
|
+
...(deps.refresh ? { refresh: deps.refresh } : {}),
|
|
88
|
+
});
|
|
89
|
+
const out = {};
|
|
90
|
+
switch (key) {
|
|
91
|
+
case "agent:bootstrap": {
|
|
92
|
+
const text = await tap.start();
|
|
93
|
+
if (text)
|
|
94
|
+
out.inject = text;
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
case "message:received":
|
|
98
|
+
await tap.user(context.content ?? "");
|
|
99
|
+
break;
|
|
100
|
+
case "message:sent": {
|
|
101
|
+
if (context.success === false)
|
|
102
|
+
break;
|
|
103
|
+
const text = context.content ?? "";
|
|
104
|
+
await tap.assistant(text);
|
|
105
|
+
if (text)
|
|
106
|
+
deps.replies.append(session, now(), text.length);
|
|
107
|
+
const nudge = tap.nudge(deps.replies.entries(session));
|
|
108
|
+
if (nudge) {
|
|
109
|
+
deps.replies.clear(session);
|
|
110
|
+
out.nudge = nudge;
|
|
111
|
+
}
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
case "command:new":
|
|
115
|
+
case "command:reset":
|
|
116
|
+
case "session:auto-reset":
|
|
117
|
+
await tap.end();
|
|
118
|
+
deps.replies.clear(session);
|
|
119
|
+
break;
|
|
120
|
+
default:
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
return JSON.stringify(out);
|
|
124
|
+
}
|