@meshpoint/cli 0.0.0-stage → 1.0.0-rc.1
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/README.md +117 -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 +310 -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 +145 -0
- package/dist/openclaw/tap.js +123 -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
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
const INJECT_WAIT_MS = 4000;
|
|
2
|
+
const WRITE_TOOLS = new Set(["edit", "write", "multiedit", "apply_patch"]);
|
|
3
|
+
function isRecord(v) {
|
|
4
|
+
return typeof v === "object" && v !== null;
|
|
5
|
+
}
|
|
6
|
+
function partsText(parts) {
|
|
7
|
+
return parts
|
|
8
|
+
.filter((p) => p.type === "text" && typeof p.text === "string" && !p.synthetic)
|
|
9
|
+
.map((p) => p.text ?? "")
|
|
10
|
+
.join("\n")
|
|
11
|
+
.trim();
|
|
12
|
+
}
|
|
13
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
|
|
14
|
+
// Maps opencode's hooks onto one Tap per session. `opencode run` exits ~20ms after session.idle
|
|
15
|
+
// without waiting for event handlers, so turns are spooled from the awaited hooks (chat.message,
|
|
16
|
+
// text.complete, tool.execute.after) and never from an HTTP read-back; events only flush, nudge
|
|
17
|
+
// and clean up. The inject goes into the system prompt on every model request of the session,
|
|
18
|
+
// which keeps it in place across turns and stable for prompt caching.
|
|
19
|
+
export function createOpencodeHooks(deps) {
|
|
20
|
+
const sessions = new Map();
|
|
21
|
+
// Subagent sessions (parentID set) are the main session's tool calls, not conversations.
|
|
22
|
+
const children = new Set();
|
|
23
|
+
const now = deps.now ?? Date.now;
|
|
24
|
+
const start = (s) => s.opened
|
|
25
|
+
.then(async () => {
|
|
26
|
+
if (s.tap)
|
|
27
|
+
s.inject = await s.tap.start();
|
|
28
|
+
})
|
|
29
|
+
.catch(() => undefined);
|
|
30
|
+
const open = (id, cwd) => {
|
|
31
|
+
const existing = sessions.get(id);
|
|
32
|
+
if (existing)
|
|
33
|
+
return existing;
|
|
34
|
+
const s = {
|
|
35
|
+
tap: null,
|
|
36
|
+
opened: Promise.resolve(),
|
|
37
|
+
started: Promise.resolve(),
|
|
38
|
+
inject: "",
|
|
39
|
+
queue: Promise.resolve(),
|
|
40
|
+
entries: [],
|
|
41
|
+
lastMessage: "",
|
|
42
|
+
paths: [],
|
|
43
|
+
nudge: null,
|
|
44
|
+
};
|
|
45
|
+
s.opened = deps
|
|
46
|
+
.openTap(id, cwd)
|
|
47
|
+
.then((tap) => {
|
|
48
|
+
s.tap = tap;
|
|
49
|
+
})
|
|
50
|
+
.catch(() => undefined);
|
|
51
|
+
s.started = start(s);
|
|
52
|
+
sessions.set(id, s);
|
|
53
|
+
return s;
|
|
54
|
+
};
|
|
55
|
+
// Mesh calls run in order per session, once the tap is open, and never surface.
|
|
56
|
+
const enqueue = (s, fn) => {
|
|
57
|
+
s.queue = s.queue
|
|
58
|
+
.then(() => s.opened)
|
|
59
|
+
.then(() => (s.tap ? fn(s.tap) : undefined))
|
|
60
|
+
.catch(() => undefined);
|
|
61
|
+
return s.queue;
|
|
62
|
+
};
|
|
63
|
+
const tracked = (id) => (id && !children.has(id) ? sessions.get(id) : undefined);
|
|
64
|
+
return {
|
|
65
|
+
event: async ({ event }) => {
|
|
66
|
+
const p = isRecord(event.properties) ? event.properties : {};
|
|
67
|
+
const info = isRecord(p.info) ? p.info : {};
|
|
68
|
+
switch (event.type) {
|
|
69
|
+
case "session.created": {
|
|
70
|
+
if (typeof info.id !== "string")
|
|
71
|
+
return;
|
|
72
|
+
if (typeof info.parentID === "string")
|
|
73
|
+
children.add(info.id);
|
|
74
|
+
else
|
|
75
|
+
open(info.id, typeof info.directory === "string" ? info.directory : deps.directory);
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
case "session.compacted": {
|
|
79
|
+
const s = tracked(typeof p.sessionID === "string" ? p.sessionID : undefined);
|
|
80
|
+
if (s)
|
|
81
|
+
s.started = start(s);
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
case "session.idle": {
|
|
85
|
+
const id = typeof p.sessionID === "string" ? p.sessionID : undefined;
|
|
86
|
+
const s = tracked(id);
|
|
87
|
+
if (!s || !id)
|
|
88
|
+
return;
|
|
89
|
+
await enqueue(s, async (tap) => {
|
|
90
|
+
await tap.flush(false);
|
|
91
|
+
const nudge = tap.nudge(s.entries);
|
|
92
|
+
if (!nudge)
|
|
93
|
+
return;
|
|
94
|
+
s.nudge = nudge;
|
|
95
|
+
await deps.client.session.promptAsync({
|
|
96
|
+
path: { id },
|
|
97
|
+
body: { ...(s.agent ? { agent: s.agent } : {}), ...(s.model ? { model: s.model } : {}), parts: [{ type: "text", text: nudge }] },
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
case "session.deleted": {
|
|
103
|
+
if (typeof info.id !== "string")
|
|
104
|
+
return;
|
|
105
|
+
children.delete(info.id);
|
|
106
|
+
const s = sessions.get(info.id);
|
|
107
|
+
sessions.delete(info.id);
|
|
108
|
+
if (s)
|
|
109
|
+
await enqueue(s, (tap) => tap.end());
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
default:
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
"chat.message": async (input, output) => {
|
|
117
|
+
if (children.has(input.sessionID))
|
|
118
|
+
return;
|
|
119
|
+
// Sessions resumed from before the plugin loaded never emitted session.created.
|
|
120
|
+
const s = open(input.sessionID, deps.directory);
|
|
121
|
+
if (input.agent)
|
|
122
|
+
s.agent = input.agent;
|
|
123
|
+
if (input.model)
|
|
124
|
+
s.model = input.model;
|
|
125
|
+
const text = partsText(output.parts);
|
|
126
|
+
if (s.nudge && text === s.nudge) {
|
|
127
|
+
s.nudge = null;
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
s.paths = [];
|
|
131
|
+
await enqueue(s, (tap) => tap.user(text));
|
|
132
|
+
},
|
|
133
|
+
"experimental.chat.system.transform": async (input, output) => {
|
|
134
|
+
const s = tracked(input.sessionID);
|
|
135
|
+
if (!s)
|
|
136
|
+
return;
|
|
137
|
+
await Promise.race([s.started, sleep(deps.injectWaitMs ?? INJECT_WAIT_MS)]);
|
|
138
|
+
if (s.inject)
|
|
139
|
+
output.system.push(s.inject);
|
|
140
|
+
},
|
|
141
|
+
"experimental.text.complete": async (input, output) => {
|
|
142
|
+
const s = tracked(input.sessionID);
|
|
143
|
+
if (!s)
|
|
144
|
+
return;
|
|
145
|
+
const text = output.text.trim();
|
|
146
|
+
const paths = s.paths;
|
|
147
|
+
s.paths = [];
|
|
148
|
+
if (!text && paths.length === 0)
|
|
149
|
+
return;
|
|
150
|
+
const last = s.entries.at(-1);
|
|
151
|
+
if (last && s.lastMessage === input.messageID) {
|
|
152
|
+
last.text = [last.text, text].filter(Boolean).join("\n\n");
|
|
153
|
+
last.paths = [...new Set([...last.paths, ...paths])];
|
|
154
|
+
last.at = now();
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
s.entries.push({ role: "assistant", text, paths, at: now() });
|
|
158
|
+
s.lastMessage = input.messageID;
|
|
159
|
+
}
|
|
160
|
+
await enqueue(s, (tap) => tap.assistant(text, paths));
|
|
161
|
+
},
|
|
162
|
+
"tool.execute.after": async (input) => {
|
|
163
|
+
const s = tracked(input.sessionID);
|
|
164
|
+
if (!s || !WRITE_TOOLS.has(input.tool) || !isRecord(input.args))
|
|
165
|
+
return;
|
|
166
|
+
const file = input.args.filePath;
|
|
167
|
+
if (typeof file === "string" && file)
|
|
168
|
+
s.paths.push(file);
|
|
169
|
+
},
|
|
170
|
+
"experimental.session.compacting": async (input) => {
|
|
171
|
+
const s = tracked(input.sessionID);
|
|
172
|
+
if (s)
|
|
173
|
+
await enqueue(s, (tap) => tap.end());
|
|
174
|
+
},
|
|
175
|
+
};
|
|
176
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// The opencode plugin entry. opencode calls every export of a plugin file as a plugin, so this
|
|
2
|
+
// module exports the plugin function and nothing else; the hook mapping lives in hooks.ts.
|
|
3
|
+
import { openTap } from "../runtime.js";
|
|
4
|
+
import { createOpencodeHooks } from "./hooks.js";
|
|
5
|
+
export const MeshpointPlugin = async (input) => createOpencodeHooks({ client: input.client, directory: input.directory, openTap });
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
import { skillMarkdown, SKILL_NAME } from "../claude-code/skill.js";
|
|
6
|
+
// pi auto-loads every *.js in <agent-dir>/extensions, so no settings.json edit is needed.
|
|
7
|
+
export function piPaths(piDir, env = process.env, home = homedir()) {
|
|
8
|
+
const dir = piDir ?? env.PI_CODING_AGENT_DIR ?? path.join(home, ".pi", "agent");
|
|
9
|
+
return {
|
|
10
|
+
extension: path.join(dir, "extensions", "meshpoint.js"),
|
|
11
|
+
skillDir: path.join(dir, "skills", SKILL_NAME),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const MARKER = "// Managed by meshpoint connect pi.";
|
|
15
|
+
// A symlink does not work: pi's loader (jiti) resolves the extension's relative imports from the
|
|
16
|
+
// link's own directory. A stub that re-exports the built file by absolute path loads it from its
|
|
17
|
+
// real location, so ../runtime.js and @meshpoint/sdk resolve from the clients install.
|
|
18
|
+
export function extensionStub(source) {
|
|
19
|
+
return `${MARKER} Re-exports the built extension so its imports resolve from the install.\nexport { default } from ${JSON.stringify(source)};\n`;
|
|
20
|
+
}
|
|
21
|
+
function builtExtension() {
|
|
22
|
+
return fileURLToPath(new URL("./extension.js", import.meta.url));
|
|
23
|
+
}
|
|
24
|
+
// true: our stub; false: someone else's file; null: absent.
|
|
25
|
+
function isManaged(file) {
|
|
26
|
+
try {
|
|
27
|
+
return readFileSync(file, "utf8").startsWith(MARKER);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function connectPi(opts) {
|
|
34
|
+
const paths = opts.paths ?? piPaths(opts.piDir);
|
|
35
|
+
if (isManaged(paths.extension) === false)
|
|
36
|
+
throw new Error(`${paths.extension} exists and was not written by meshpoint; remove it manually`);
|
|
37
|
+
if (opts.remove) {
|
|
38
|
+
rmSync(paths.extension, { force: true });
|
|
39
|
+
rmSync(paths.skillDir, { recursive: true, force: true });
|
|
40
|
+
return `removed ${paths.extension} and ${paths.skillDir}`;
|
|
41
|
+
}
|
|
42
|
+
const source = opts.source ?? builtExtension();
|
|
43
|
+
if (!existsSync(source))
|
|
44
|
+
throw new Error(`${source} is missing; run npm run build first`);
|
|
45
|
+
mkdirSync(path.dirname(paths.extension), { recursive: true });
|
|
46
|
+
writeFileSync(paths.extension, extensionStub(source));
|
|
47
|
+
mkdirSync(paths.skillDir, { recursive: true });
|
|
48
|
+
writeFileSync(path.join(paths.skillDir, "SKILL.md"), skillMarkdown({ allowedTools: false }));
|
|
49
|
+
return `wrote ${paths.extension} (loads ${source}) and ${paths.skillDir}/SKILL.md. Restart pi (or /reload) to load it.`;
|
|
50
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { openTap } from "../runtime.js";
|
|
3
|
+
export const INJECT_TYPE = "meshpoint";
|
|
4
|
+
export const NUDGE_TYPE = "meshpoint-nudge";
|
|
5
|
+
const EDITING_TOOLS = new Set(["edit", "write"]);
|
|
6
|
+
export function messageText(content) {
|
|
7
|
+
if (typeof content === "string")
|
|
8
|
+
return content.trim();
|
|
9
|
+
return content
|
|
10
|
+
.map((c) => (c.type === "text" && typeof c.text === "string" ? c.text : ""))
|
|
11
|
+
.filter(Boolean)
|
|
12
|
+
.join("\n")
|
|
13
|
+
.trim();
|
|
14
|
+
}
|
|
15
|
+
// Assistant replies already on the session branch, so a resumed session counts toward its
|
|
16
|
+
// checkpoint from where it left off.
|
|
17
|
+
export function assistantEntries(branch) {
|
|
18
|
+
const out = [];
|
|
19
|
+
for (const e of branch) {
|
|
20
|
+
if (e.type !== "message" || e.message?.role !== "assistant")
|
|
21
|
+
continue;
|
|
22
|
+
const text = messageText(e.message.content);
|
|
23
|
+
if (text)
|
|
24
|
+
out.push({ role: "assistant", text, paths: [], at: e.message.timestamp ?? 0 });
|
|
25
|
+
}
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
// Event mapping: session_start opens the Tap and starts recall in the background; the first
|
|
29
|
+
// user run injects it as a hidden custom message and records the prompt; turn_end and tool_call
|
|
30
|
+
// collect the run's reply text and edited files; agent_before_settle spools the reply and, when a
|
|
31
|
+
// checkpoint is due, appends the nudge and asks for one more model request; compaction and
|
|
32
|
+
// shutdown flush. Every handler swallows failures: the mesh must never break pi.
|
|
33
|
+
export function meshpointExtension(pi, deps = {}) {
|
|
34
|
+
const open = deps.open ?? openTap;
|
|
35
|
+
const now = deps.now ?? Date.now;
|
|
36
|
+
let tap = null;
|
|
37
|
+
let inject = null;
|
|
38
|
+
let entries = [];
|
|
39
|
+
let texts = [];
|
|
40
|
+
let paths = [];
|
|
41
|
+
const quiet = async (work) => {
|
|
42
|
+
try {
|
|
43
|
+
return await work();
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
pi.on("session_start", (_event, ctx) => quiet(async () => {
|
|
50
|
+
tap = await open(ctx.sessionManager.getSessionId(), ctx.cwd);
|
|
51
|
+
if (!tap)
|
|
52
|
+
return;
|
|
53
|
+
entries = assistantEntries(ctx.sessionManager.getBranch());
|
|
54
|
+
texts = [];
|
|
55
|
+
paths = [];
|
|
56
|
+
inject = tap.start();
|
|
57
|
+
}));
|
|
58
|
+
pi.on("before_agent_start", (event) => quiet(async () => {
|
|
59
|
+
if (!tap || event.messageType !== "user")
|
|
60
|
+
return undefined;
|
|
61
|
+
await tap.user(event.prompt);
|
|
62
|
+
if (!inject)
|
|
63
|
+
return undefined;
|
|
64
|
+
const text = await inject;
|
|
65
|
+
inject = null;
|
|
66
|
+
return text ? { message: { customType: INJECT_TYPE, content: text, display: false } } : undefined;
|
|
67
|
+
}));
|
|
68
|
+
pi.on("turn_end", (event) => {
|
|
69
|
+
if (!tap || event.message.role !== "assistant")
|
|
70
|
+
return undefined;
|
|
71
|
+
const text = messageText(event.message.content);
|
|
72
|
+
if (text)
|
|
73
|
+
texts.push(text);
|
|
74
|
+
return undefined;
|
|
75
|
+
});
|
|
76
|
+
pi.on("tool_call", (event, ctx) => {
|
|
77
|
+
const p = event.input.path;
|
|
78
|
+
if (tap && EDITING_TOOLS.has(event.toolName) && typeof p === "string" && p)
|
|
79
|
+
paths.push(path.resolve(ctx.cwd, p));
|
|
80
|
+
return undefined;
|
|
81
|
+
});
|
|
82
|
+
pi.on("agent_before_settle", (event) => quiet(async () => {
|
|
83
|
+
if (!tap)
|
|
84
|
+
return undefined;
|
|
85
|
+
const text = texts.join("\n\n");
|
|
86
|
+
const touched = [...new Set(paths)];
|
|
87
|
+
texts = [];
|
|
88
|
+
paths = [];
|
|
89
|
+
if (!text && touched.length === 0)
|
|
90
|
+
return undefined;
|
|
91
|
+
await tap.assistant(text, touched);
|
|
92
|
+
entries.push({ role: "assistant", text, paths: touched, at: now() });
|
|
93
|
+
if (event.outcome !== "completed")
|
|
94
|
+
return undefined;
|
|
95
|
+
const nudge = tap.nudge(entries);
|
|
96
|
+
if (!nudge)
|
|
97
|
+
return undefined;
|
|
98
|
+
return { entries: [{ type: "custom_message", customType: NUDGE_TYPE, content: nudge, display: true }], continue: true };
|
|
99
|
+
}));
|
|
100
|
+
pi.on("session_before_compact", () => quiet(async () => {
|
|
101
|
+
await tap?.end();
|
|
102
|
+
return undefined;
|
|
103
|
+
}));
|
|
104
|
+
// The injected recall may have been summarized away; fetch it again for the next prompt.
|
|
105
|
+
pi.on("session_compact", () => {
|
|
106
|
+
if (tap)
|
|
107
|
+
inject = tap.start();
|
|
108
|
+
});
|
|
109
|
+
pi.on("session_shutdown", () => quiet(async () => {
|
|
110
|
+
const closing = tap;
|
|
111
|
+
tap = null;
|
|
112
|
+
inject = null;
|
|
113
|
+
await closing?.end();
|
|
114
|
+
}));
|
|
115
|
+
}
|
|
116
|
+
export default function (pi) {
|
|
117
|
+
meshpointExtension(pi);
|
|
118
|
+
}
|
package/dist/runtime.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Checkpoints } from "./checkpoints.js";
|
|
2
|
+
import { loadConfig, repoContext, spoolDir, stateDir } from "./config.js";
|
|
3
|
+
import { freshConfig } from "./control.js";
|
|
4
|
+
import { Spool } from "./spool.js";
|
|
5
|
+
import { Tap } from "./tap.js";
|
|
6
|
+
// A Tap for one harness session, wired to the stored login and the on-disk spool. Null when the
|
|
7
|
+
// machine is not logged in or the directory is not a repo with an origin remote. In-process
|
|
8
|
+
// adapters (pi, opencode, openclaw) call this once per harness session.
|
|
9
|
+
export async function openTap(session, cwd) {
|
|
10
|
+
const stored = loadConfig();
|
|
11
|
+
if (!stored)
|
|
12
|
+
return null;
|
|
13
|
+
const repo = repoContext(cwd);
|
|
14
|
+
if (!repo)
|
|
15
|
+
return null;
|
|
16
|
+
// A failed refresh keeps the stored token: the adapter must never break the harness.
|
|
17
|
+
const config = await freshConfig(stored).catch(() => stored);
|
|
18
|
+
return new Tap({
|
|
19
|
+
config,
|
|
20
|
+
repo,
|
|
21
|
+
session,
|
|
22
|
+
spool: new Spool(spoolDir()),
|
|
23
|
+
checkpoints: new Checkpoints(stateDir()),
|
|
24
|
+
refresh: () => freshConfig(config, { force: true }).catch(() => null),
|
|
25
|
+
});
|
|
26
|
+
}
|
package/dist/spool.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { appendFileSync, mkdirSync, readFileSync, renameSync, rmSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
export const FLUSH_AFTER_MS = 60_000;
|
|
4
|
+
export class Spool {
|
|
5
|
+
dir;
|
|
6
|
+
constructor(dir) {
|
|
7
|
+
this.dir = dir;
|
|
8
|
+
}
|
|
9
|
+
file(session) {
|
|
10
|
+
return path.join(this.dir, `${session.replace(/[^A-Za-z0-9._-]/g, "_")}.jsonl`);
|
|
11
|
+
}
|
|
12
|
+
append(session, turn) {
|
|
13
|
+
mkdirSync(this.dir, { recursive: true, mode: 0o700 });
|
|
14
|
+
appendFileSync(this.file(session), `${JSON.stringify(turn)}\n`, { mode: 0o600 });
|
|
15
|
+
}
|
|
16
|
+
read(session) {
|
|
17
|
+
let raw;
|
|
18
|
+
try {
|
|
19
|
+
raw = readFileSync(this.file(session), "utf8");
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return [];
|
|
23
|
+
}
|
|
24
|
+
const out = [];
|
|
25
|
+
for (const line of raw.split("\n")) {
|
|
26
|
+
if (!line.trim())
|
|
27
|
+
continue;
|
|
28
|
+
try {
|
|
29
|
+
out.push(JSON.parse(line));
|
|
30
|
+
}
|
|
31
|
+
catch {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
isDue(session, now, force) {
|
|
38
|
+
const turns = this.read(session);
|
|
39
|
+
if (turns.length === 0)
|
|
40
|
+
return false;
|
|
41
|
+
return force || now - (turns[0]?.at ?? now) >= FLUSH_AFTER_MS;
|
|
42
|
+
}
|
|
43
|
+
// Moves the file aside first so a hook running concurrently appends to a fresh file; the
|
|
44
|
+
// caller sends the returned turns and calls `discard` on success or `restore` on failure.
|
|
45
|
+
take(session) {
|
|
46
|
+
const file = this.file(session);
|
|
47
|
+
const taken = `${file}.${process.pid}.${Date.now()}.sending`;
|
|
48
|
+
try {
|
|
49
|
+
renameSync(file, taken);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return { turns: [], discard: () => undefined, restore: () => undefined };
|
|
53
|
+
}
|
|
54
|
+
const turns = this.readFile(taken);
|
|
55
|
+
return {
|
|
56
|
+
turns,
|
|
57
|
+
discard: () => rmSync(taken, { force: true }),
|
|
58
|
+
restore: () => {
|
|
59
|
+
// Prepend what was taken to whatever arrived meanwhile, keeping order.
|
|
60
|
+
const newer = this.read(session);
|
|
61
|
+
rmSync(file, { force: true });
|
|
62
|
+
for (const t of [...turns, ...newer])
|
|
63
|
+
this.append(session, t);
|
|
64
|
+
rmSync(taken, { force: true });
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
readFile(file) {
|
|
69
|
+
try {
|
|
70
|
+
return readFileSync(file, "utf8")
|
|
71
|
+
.split("\n")
|
|
72
|
+
.filter((l) => l.trim())
|
|
73
|
+
.map((l) => JSON.parse(l));
|
|
74
|
+
}
|
|
75
|
+
catch {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
package/dist/tap.js
ADDED
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { MeshpointClient, MeshpointError, redact } from "@meshpoint/sdk";
|
|
3
|
+
import { SKILL_NAME } from "./claude-code/skill.js";
|
|
4
|
+
import { ID_PREFIX_CHARS } from "./items.js";
|
|
5
|
+
const DEFAULT_PRESENCE_WAIT_MS = 1500;
|
|
6
|
+
// Harnesses that feed the nudge back as a user prompt (Cursor) match on this to keep it out of the capture.
|
|
7
|
+
export const NUDGE_PREFIX = "Meshpoint checkpoint due";
|
|
8
|
+
// One harness session's side of the mesh, independent of how the harness delivers events: recall
|
|
9
|
+
// and presence rendered for injection at start, turns spooled and flushed in 60s batches, and the
|
|
10
|
+
// checkpoint nudge once enough replies pile up. Every method swallows mesh failures: a broken
|
|
11
|
+
// mesh must never break the harness.
|
|
12
|
+
export class Tap {
|
|
13
|
+
deps;
|
|
14
|
+
config;
|
|
15
|
+
constructor(deps) {
|
|
16
|
+
this.deps = deps;
|
|
17
|
+
this.config = deps.config;
|
|
18
|
+
}
|
|
19
|
+
get repo() {
|
|
20
|
+
return this.deps.repo;
|
|
21
|
+
}
|
|
22
|
+
// The context block for the start of a session, or "" when there is nothing to say.
|
|
23
|
+
async start() {
|
|
24
|
+
try {
|
|
25
|
+
const { recall, presence } = await this.client(this.repo.branch)
|
|
26
|
+
.sessionStart()
|
|
27
|
+
.catch(async (err) => {
|
|
28
|
+
if (!is401(err) || !(await this.reauth()))
|
|
29
|
+
throw err;
|
|
30
|
+
return this.client(this.repo.branch).sessionStart();
|
|
31
|
+
});
|
|
32
|
+
const teammates = await Promise.race([
|
|
33
|
+
presence,
|
|
34
|
+
new Promise((r) => setTimeout(() => r([]), this.deps.presenceWaitMs ?? DEFAULT_PRESENCE_WAIT_MS)),
|
|
35
|
+
]);
|
|
36
|
+
const text = renderInject(this.repo, recall, teammates, this.now());
|
|
37
|
+
await this.flush(false);
|
|
38
|
+
return text;
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return "";
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async user(prompt) {
|
|
45
|
+
if (prompt)
|
|
46
|
+
this.spoolTurn(`[user] ${prompt}`);
|
|
47
|
+
await this.flush(false);
|
|
48
|
+
}
|
|
49
|
+
async assistant(text, paths = []) {
|
|
50
|
+
if (text || paths.length > 0)
|
|
51
|
+
this.spoolTurn(`[assistant] ${text}`, paths);
|
|
52
|
+
await this.flush(false);
|
|
53
|
+
}
|
|
54
|
+
// The nudge to show the agent when a checkpoint is due, else null. Recording the nudge keeps an
|
|
55
|
+
// ignored one from repeating on every reply.
|
|
56
|
+
nudge(entries) {
|
|
57
|
+
const due = this.deps.checkpoints.due(this.deps.session, this.repo.project, this.repo.branch, entries);
|
|
58
|
+
if (!due)
|
|
59
|
+
return null;
|
|
60
|
+
this.deps.checkpoints.markNudge(this.deps.session, this.now());
|
|
61
|
+
return `${NUDGE_PREFIX} (${due.turns} replies since the last one). Run the ${SKILL_NAME} skill now, then continue.`;
|
|
62
|
+
}
|
|
63
|
+
// Session end and pre-compaction: send whatever is spooled now.
|
|
64
|
+
async end() {
|
|
65
|
+
await this.flush(true);
|
|
66
|
+
}
|
|
67
|
+
async flush(force) {
|
|
68
|
+
try {
|
|
69
|
+
if (!this.deps.spool.isDue(this.deps.session, this.now(), force))
|
|
70
|
+
return;
|
|
71
|
+
const taken = this.deps.spool.take(this.deps.session);
|
|
72
|
+
let result = await this.send(taken.turns);
|
|
73
|
+
if (result === "unauthorized" && (await this.reauth()))
|
|
74
|
+
result = await this.send(taken.turns);
|
|
75
|
+
if (result === "ok")
|
|
76
|
+
taken.discard();
|
|
77
|
+
else
|
|
78
|
+
taken.restore();
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
// The spool keeps the turns for the next event.
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
now() {
|
|
85
|
+
return (this.deps.now ?? Date.now)();
|
|
86
|
+
}
|
|
87
|
+
client(branch, onError) {
|
|
88
|
+
return new MeshpointClient({
|
|
89
|
+
baseUrl: this.config.baseUrl,
|
|
90
|
+
token: this.config.token,
|
|
91
|
+
project: this.repo.project,
|
|
92
|
+
branch,
|
|
93
|
+
...(this.deps.fetch ? { fetch: this.deps.fetch } : {}),
|
|
94
|
+
...(onError ? { onError } : {}),
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
async reauth() {
|
|
98
|
+
const next = this.deps.refresh ? await this.deps.refresh() : null;
|
|
99
|
+
if (!next)
|
|
100
|
+
return false;
|
|
101
|
+
this.config = next;
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
// Credentials are masked before the turn touches the spool file, so nothing on disk or on the
|
|
105
|
+
// wire holds them. The SDK masks again on send for callers that bypass the spool.
|
|
106
|
+
spoolTurn(body, paths = []) {
|
|
107
|
+
const rel = paths.map((p) => (path.isAbsolute(p) ? path.relative(this.repo.root, p) : p)).filter((p) => p && !p.startsWith(".."));
|
|
108
|
+
const turn = { branch: this.repo.branch, body: redact(body), at: this.now(), ...(rel.length ? { paths: rel } : {}) };
|
|
109
|
+
this.deps.spool.append(this.deps.session, turn);
|
|
110
|
+
}
|
|
111
|
+
// The SDK reports a failed flush through onError instead of throwing.
|
|
112
|
+
async send(turns) {
|
|
113
|
+
const byBranch = new Map();
|
|
114
|
+
for (const t of turns)
|
|
115
|
+
byBranch.set(t.branch, [...(byBranch.get(t.branch) ?? []), t]);
|
|
116
|
+
let failed = false;
|
|
117
|
+
let unauthorized = false;
|
|
118
|
+
const note = (err) => {
|
|
119
|
+
failed = true;
|
|
120
|
+
if (is401(err))
|
|
121
|
+
unauthorized = true;
|
|
122
|
+
};
|
|
123
|
+
try {
|
|
124
|
+
for (const [branch, batch] of byBranch) {
|
|
125
|
+
const c = this.client(branch, note);
|
|
126
|
+
for (const t of batch)
|
|
127
|
+
c.capture({ body: t.body, at: t.at, ...(t.paths ? { paths: t.paths } : {}) });
|
|
128
|
+
await c.close();
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (err) {
|
|
132
|
+
note(err);
|
|
133
|
+
}
|
|
134
|
+
return failed ? (unauthorized ? "unauthorized" : "failed") : "ok";
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
function is401(err) {
|
|
138
|
+
return err instanceof MeshpointError && err.status === 401;
|
|
139
|
+
}
|
|
140
|
+
function age(ms) {
|
|
141
|
+
const m = Math.floor(ms / 60_000);
|
|
142
|
+
if (m < 1)
|
|
143
|
+
return "just now";
|
|
144
|
+
if (m < 60)
|
|
145
|
+
return `${m}m ago`;
|
|
146
|
+
const h = Math.floor(m / 60);
|
|
147
|
+
return h < 48 ? `${h}h ago` : `${Math.floor(h / 24)}d ago`;
|
|
148
|
+
}
|
|
149
|
+
export function renderInject(repo, recall, presence, now) {
|
|
150
|
+
if (recall.items.length === 0 && !recall.checkpoint && presence.length === 0)
|
|
151
|
+
return "";
|
|
152
|
+
const lines = [`# Meshpoint: ${repo.project} @ ${repo.branch}`];
|
|
153
|
+
if (presence.length > 0) {
|
|
154
|
+
lines.push("", "## Teammates active now");
|
|
155
|
+
for (const p of presence)
|
|
156
|
+
lines.push(`- ${p.user} (${p.agent}, ${age(now - p.updated_at)})${p.paths.length ? `: ${p.paths.join(", ")}` : ""}`);
|
|
157
|
+
}
|
|
158
|
+
if (recall.checkpoint) {
|
|
159
|
+
lines.push("", `## Last checkpoint (${age(now - recall.checkpoint.created_at)})`, recall.checkpoint.body);
|
|
160
|
+
}
|
|
161
|
+
if (recall.items.length > 0) {
|
|
162
|
+
lines.push("", `## Team knowledge (${recall.items.length}; first column is the item id)`);
|
|
163
|
+
for (const i of recall.items)
|
|
164
|
+
lines.push(`- ${i.id.slice(0, ID_PREFIX_CHARS)} [${i.kind}] ${i.title}: ${i.body}`);
|
|
165
|
+
}
|
|
166
|
+
return lines.join("\n");
|
|
167
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meshpoint/cli",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0-rc.1",
|
|
4
|
+
"description": "Meshpoint CLI and harness adapters",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"meshpoint",
|
|
7
|
+
"coding-agents",
|
|
8
|
+
"claude-code",
|
|
9
|
+
"codex",
|
|
10
|
+
"cursor",
|
|
11
|
+
"memory"
|
|
12
|
+
],
|
|
13
|
+
"license": "UNLICENSED",
|
|
14
|
+
"homepage": "https://meshpoint.dev",
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/meshpoint-dev/clients.git"
|
|
18
|
+
},
|
|
19
|
+
"type": "module",
|
|
20
|
+
"engines": {
|
|
21
|
+
"node": ">=22"
|
|
22
|
+
},
|
|
23
|
+
"bin": {
|
|
24
|
+
"meshpoint": "dist/bin.js"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
],
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public",
|
|
31
|
+
"tag": "next"
|
|
32
|
+
},
|
|
33
|
+
"scripts": {
|
|
34
|
+
"build": "tsc -p tsconfig.build.json",
|
|
35
|
+
"typecheck": "tsc --noEmit -p .",
|
|
36
|
+
"test": "vitest run",
|
|
37
|
+
"check": "npm run typecheck && npm test",
|
|
38
|
+
"prepublishOnly": "npm run check && npm run build"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@meshpoint/sdk": "^1.0.0-rc.1"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^22.0.0",
|
|
45
|
+
"typescript": "^5.6.0",
|
|
46
|
+
"vitest": "4.1.12"
|
|
47
|
+
}
|
|
48
|
+
}
|