virtualmatter 0.6.4 → 0.6.7
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 +39 -7
- package/dist/agent-logs.js +33 -5
- package/dist/agentfiles.js +19 -7
- package/dist/agents-md.generated.js +1 -1
- package/dist/atomo.js +157 -0
- package/dist/files.js +200 -19
- package/dist/ignore.js +15 -3
- package/dist/index.js +248 -24
- package/dist/lock.js +208 -0
- package/dist/log-events.js +2 -2
- package/dist/mcp.js +137 -3
- package/dist/pull.js +20 -4
- package/dist/skills.js +73 -0
- package/dist/sync.js +322 -38
- package/dist/world-status.js +161 -0
- package/package.json +1 -1
package/dist/mcp.js
CHANGED
|
@@ -17,10 +17,13 @@ import { z } from "zod";
|
|
|
17
17
|
import { createProject, createWebsiteBuild, getWebsiteBuild, getEmbed, defaultRegion, editUrl, listProjects, playUrl, resolveEditSession, resolveSession, sessionBaseUrl, } from "./api.js";
|
|
18
18
|
import { ensureClientInstalled, launchClient, seedNativeAuth } from "./client.js";
|
|
19
19
|
import { packageVersion } from "./config.js";
|
|
20
|
-
import { ConflictError,
|
|
20
|
+
import { ConflictError, editSessionClient } from "./files.js";
|
|
21
|
+
import { acquireFolderLock } from "./lock.js";
|
|
21
22
|
import { pullCommand } from "./pull.js";
|
|
22
23
|
import { makeFramingOf, resolveTarget } from "./resolve.js";
|
|
23
24
|
import { loadState } from "./state.js";
|
|
25
|
+
import { SyncEngine } from "./sync.js";
|
|
26
|
+
import { bootSceneNotes, getDrift, getEngineLog, getEngineRestarts, getPublishRun, projectForEditor, startPublish, } from "./world-status.js";
|
|
24
27
|
export function resolveFramingId(dir, framingFlag) {
|
|
25
28
|
if (framingFlag)
|
|
26
29
|
return framingFlag;
|
|
@@ -91,8 +94,9 @@ function makeContext(framingId) {
|
|
|
91
94
|
return session;
|
|
92
95
|
},
|
|
93
96
|
async getClient() {
|
|
97
|
+
// a moved session (engine restart) also invalidates the cached lookup world_info reports
|
|
94
98
|
if (!client)
|
|
95
|
-
client =
|
|
99
|
+
client = editSessionClient(ctx.requireFraming(), await ctx.getSession(), () => (session = null));
|
|
96
100
|
return client;
|
|
97
101
|
},
|
|
98
102
|
};
|
|
@@ -134,6 +138,35 @@ export async function writeWithRetry(client, filePath, body) {
|
|
|
134
138
|
return await client.put(filePath, body, etag ? { etag } : { createOnly: true });
|
|
135
139
|
}
|
|
136
140
|
}
|
|
141
|
+
/**
|
|
142
|
+
* patch_file: replace exactly one occurrence of `oldText` in a world file.
|
|
143
|
+
* The write is conditional on the version just read; if the file moved in
|
|
144
|
+
* between, it is read again and patched once more (the anchor must still be
|
|
145
|
+
* unique there), so a concurrent edit elsewhere in the file survives.
|
|
146
|
+
*/
|
|
147
|
+
export async function patchRemoteFile(client, filePath, oldText, newText) {
|
|
148
|
+
if (oldText.length === 0)
|
|
149
|
+
throw new Error("old_text must not be empty.");
|
|
150
|
+
for (let attempt = 0;; attempt++) {
|
|
151
|
+
const { bytes, etag } = await client.get(filePath);
|
|
152
|
+
const text = bytes.toString("utf8");
|
|
153
|
+
const first = text.indexOf(oldText);
|
|
154
|
+
if (first === -1)
|
|
155
|
+
throw new Error(`old_text was not found in ${filePath}. Read the file and copy the text exactly, whitespace included.`);
|
|
156
|
+
if (text.indexOf(oldText, first + 1) !== -1) {
|
|
157
|
+
throw new Error(`old_text occurs more than once in ${filePath}; include more surrounding lines so it is unique.`);
|
|
158
|
+
}
|
|
159
|
+
const next = Buffer.from(text.slice(0, first) + newText + text.slice(first + oldText.length), "utf8");
|
|
160
|
+
try {
|
|
161
|
+
const written = await client.put(filePath, next, etag ? { etag } : {});
|
|
162
|
+
return { etag: written, size: next.length };
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
if (!(err instanceof ConflictError) || attempt >= 1)
|
|
166
|
+
throw err;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
137
170
|
export async function runMcpServer(dir, framingFlag) {
|
|
138
171
|
const ctx = makeContext(resolveFramingId(dir, framingFlag));
|
|
139
172
|
// Instructions land in the host agent's system prompt, which is the only
|
|
@@ -238,7 +271,7 @@ export async function runMcpServer(dir, framingFlag) {
|
|
|
238
271
|
return textResult(bytes.toString("utf8"));
|
|
239
272
|
});
|
|
240
273
|
server.registerTool("write_file", {
|
|
241
|
-
description: "Write (create or overwrite) one file in the live session's Montage tree with the full desired content. Concurrency is handled internally with etags - if someone else changed the file since you read it, the write retries once against the latest version and your content wins. Lua scripts hot-reload in the engine on write, so writing a script IS deploying it.",
|
|
274
|
+
description: "Write (create or overwrite) one file in the live session's Montage tree with the full desired content. Concurrency is handled internally with etags - if someone else changed the file since you read it, the write retries once against the latest version and your content wins. Lua scripts hot-reload in the engine on write, so writing a script IS deploying it. The whole file travels in this request: to change part of a big script use patch_file, and to deploy files you edited on disk use push_local_files.",
|
|
242
275
|
inputSchema: {
|
|
243
276
|
path: z.string().describe("Relative file path inside the montage tree"),
|
|
244
277
|
content: z.string().describe("Full new file content (UTF-8 text)"),
|
|
@@ -248,6 +281,107 @@ export async function runMcpServer(dir, framingFlag) {
|
|
|
248
281
|
const etag = await writeWithRetry(client, filePath, Buffer.from(content, "utf8"));
|
|
249
282
|
return textResult(JSON.stringify({ ok: true, path: filePath, etag }));
|
|
250
283
|
});
|
|
284
|
+
server.registerTool("patch_file", {
|
|
285
|
+
description: "Edit one file in the live session's Montage tree by replacing an exact, unique snippet - without sending the whole file. old_text must appear exactly once (copy it from read_file, whitespace included; add surrounding lines to make it unique). Like write_file, a Lua script hot-reloads on write.",
|
|
286
|
+
inputSchema: {
|
|
287
|
+
path: z.string().describe("Relative file path inside the montage tree"),
|
|
288
|
+
old_text: z.string().describe("The exact text to replace; must occur exactly once"),
|
|
289
|
+
new_text: z.string().describe("The replacement text"),
|
|
290
|
+
},
|
|
291
|
+
}, async ({ path: filePath, old_text, new_text }) => {
|
|
292
|
+
const client = await ctx.getClient();
|
|
293
|
+
const res = await patchRemoteFile(client, filePath, old_text, new_text);
|
|
294
|
+
return textResult(JSON.stringify({ ok: true, path: filePath, ...res }));
|
|
295
|
+
});
|
|
296
|
+
/** The synced folder this server was started in, checked against the selected world. */
|
|
297
|
+
const syncedFolder = () => {
|
|
298
|
+
const state = loadState(dir);
|
|
299
|
+
if (!state) {
|
|
300
|
+
throw new Error(`${dir} is not a synced folder (no .virtualmatter.json). Mirror the world first: select_project with pull_to, or \`npx virtualmatter pull\`.`);
|
|
301
|
+
}
|
|
302
|
+
if (ctx.framingId && state.framing_id !== ctx.framingId) {
|
|
303
|
+
throw new Error(`${dir} mirrors framing ${state.framing_id}, but the selected world is ${ctx.framingId}.`);
|
|
304
|
+
}
|
|
305
|
+
return state;
|
|
306
|
+
};
|
|
307
|
+
server.registerTool("push_local_files", {
|
|
308
|
+
description: "Deploy files from the local synced folder this server runs in: pushes the named paths (or, with none, every file changed locally since the last sync) and reports each one as pushed, unchanged, conflict, too-large, missing or failed. The file contents never pass through this request, so large scripts cost no context. Unchanged files are not sent, so they do not reset the world's Lua. Refuses while `virtualmatter sync` runs on the folder (it already pushes every save).",
|
|
309
|
+
inputSchema: {
|
|
310
|
+
paths: z.array(z.string()).optional().describe("Paths relative to the synced folder; omit for every changed file"),
|
|
311
|
+
force: z.boolean().optional().describe("Overwrite the world's copy even if it changed since the last sync"),
|
|
312
|
+
},
|
|
313
|
+
}, async ({ paths, force }) => {
|
|
314
|
+
const state = syncedFolder();
|
|
315
|
+
const release = await acquireFolderLock(dir, "push", { log: () => { } });
|
|
316
|
+
try {
|
|
317
|
+
const engine = new SyncEngine(await ctx.getClient(), dir, state, { logger: { info: () => { }, warn: () => { } } });
|
|
318
|
+
return textResult(JSON.stringify(await engine.pushPaths(paths && paths.length > 0 ? paths : null, { force }), null, 2));
|
|
319
|
+
}
|
|
320
|
+
finally {
|
|
321
|
+
release();
|
|
322
|
+
}
|
|
323
|
+
});
|
|
324
|
+
server.registerTool("fetch_files", {
|
|
325
|
+
description: "Download files from the world into the local synced folder this server runs in: the named paths (any size), or with none every file the world changed since the last sync (except files too large to push back, such as terrain). Never overwrites local edits; a file changed on both sides gets a <name>.remote-conflict copy. Reports each file.",
|
|
326
|
+
inputSchema: {
|
|
327
|
+
paths: z.array(z.string()).optional().describe("Paths relative to the synced folder; omit for every file the world changed"),
|
|
328
|
+
},
|
|
329
|
+
}, async ({ paths }) => {
|
|
330
|
+
const state = syncedFolder();
|
|
331
|
+
const release = await acquireFolderLock(dir, "fetch", { log: () => { } });
|
|
332
|
+
try {
|
|
333
|
+
const engine = new SyncEngine(await ctx.getClient(), dir, state, { logger: { info: () => { }, warn: () => { } } });
|
|
334
|
+
return textResult(JSON.stringify(await engine.fetchPaths(paths && paths.length > 0 ? paths : null), null, 2));
|
|
335
|
+
}
|
|
336
|
+
finally {
|
|
337
|
+
release();
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
server.registerTool("get_engine_logs", {
|
|
341
|
+
description: "The engine's log for the selected world. With restarts=true: every recent engine restart and why - a crash (the engine restarted itself; with the last lines it logged) or a platform stop with its reason (idle, update, restart, out of memory). Otherwise the last `tail` lines of the running engine's log. Check restarts when the world dropped players, lost recent changes, or get_engine_errors came back suspiciously empty (a restart clears it). framing_id targets another framing of the world, such as a play server from play_server_status.",
|
|
342
|
+
inputSchema: {
|
|
343
|
+
restarts: z.boolean().optional().describe("List recent restarts and their reasons instead of the log tail"),
|
|
344
|
+
tail: z.number().int().min(0).max(2000).optional().describe("Lines (per restart with restarts=true); default 100, or 20 per restart"),
|
|
345
|
+
framing_id: z.string().optional().describe("Another framing of this world, e.g. a play server"),
|
|
346
|
+
},
|
|
347
|
+
}, async ({ restarts, tail, framing_id }) => {
|
|
348
|
+
await ctx.getSession(); // settles a play-server selection onto the editor
|
|
349
|
+
const framing = framing_id ?? ctx.requireFraming();
|
|
350
|
+
if (restarts)
|
|
351
|
+
return textResult(JSON.stringify(await getEngineRestarts(framing, Math.min(tail ?? 20, 200)), null, 2));
|
|
352
|
+
return textResult(JSON.stringify(await getEngineLog(framing, Math.max(1, tail ?? 100)), null, 2));
|
|
353
|
+
});
|
|
354
|
+
server.registerTool("play_server_status", {
|
|
355
|
+
description: "The selected world's play servers (what visitors play): whether each still matches the editor, which scene they load and when the editor last saved it, and how the last update went (per server). Use it before and after publish_play_servers.",
|
|
356
|
+
inputSchema: {},
|
|
357
|
+
}, async () => {
|
|
358
|
+
await ctx.getSession();
|
|
359
|
+
const project = await projectForEditor(ctx.requireFraming());
|
|
360
|
+
const [drift, last] = await Promise.all([getDrift(project.id), getPublishRun(project.id)]);
|
|
361
|
+
const notes = bootSceneNotes(drift, last?.send_played_scene ?? false);
|
|
362
|
+
return textResult(JSON.stringify({ project: project.name, boot_scene: notes.facts, drift, last_update: last }, null, 2));
|
|
363
|
+
});
|
|
364
|
+
server.registerTool("publish_play_servers", {
|
|
365
|
+
description: "Publish the editor to the world's play servers (the project page's Update play servers): saves and pushes the editor's changes, copies its voxel data, and restarts each play server on it. Returns at once with the run and any warning about which scene the servers will load; poll play_server_status until last_update.status is done or failed (it can take minutes for a large terrain). Anyone playing briefly reconnects.",
|
|
366
|
+
inputSchema: {
|
|
367
|
+
send_played_scene: z
|
|
368
|
+
.boolean()
|
|
369
|
+
.optional()
|
|
370
|
+
.describe("Send the editor's played scene and its terrain instead of InitScene (projects that persist scene changes)"),
|
|
371
|
+
},
|
|
372
|
+
}, async ({ send_played_scene }) => {
|
|
373
|
+
await ctx.getSession();
|
|
374
|
+
const project = await projectForEditor(ctx.requireFraming());
|
|
375
|
+
let notes = null;
|
|
376
|
+
try {
|
|
377
|
+
notes = bootSceneNotes(await getDrift(project.id), send_played_scene === true);
|
|
378
|
+
}
|
|
379
|
+
catch {
|
|
380
|
+
notes = null;
|
|
381
|
+
}
|
|
382
|
+
const run = await startPublish(project.id, send_played_scene === true);
|
|
383
|
+
return textResult(JSON.stringify({ started: run, boot_scene: notes?.facts ?? null, warnings: notes?.warnings ?? [] }, null, 2));
|
|
384
|
+
});
|
|
251
385
|
server.registerTool("run_lua", {
|
|
252
386
|
description: "Execute a Lua snippet in the running Virtual Matter engine and return its result. target \"server\" (default) runs in the server-side Lua state where game logic lives; \"client\" runs in a connected client (someone must have the world open) - that is where UI, input and the camera live. With several clients connected, pass client_id; list them with target server and code 'return Server:GetClients()'. Use this to inspect live state, call engine APIs, or nudge the scene without editing files. If it fails with \"run-lua timed out\", the engine may still be running the snippet: check its effect before sending code that changes the world again.",
|
|
253
387
|
inputSchema: {
|
package/dist/pull.js
CHANGED
|
@@ -3,7 +3,9 @@ import fs from "node:fs";
|
|
|
3
3
|
import path from "node:path";
|
|
4
4
|
import { resolveEditSession, sessionBaseUrl } from "./api.js";
|
|
5
5
|
import { composeAgentsMd, fetchAgentsMd, writeAgentFiles } from "./agentfiles.js";
|
|
6
|
-
import {
|
|
6
|
+
import { writeAtomoWrapper } from "./atomo.js";
|
|
7
|
+
import { mirrorSkills, skillsActive } from "./skills.js";
|
|
8
|
+
import { editSessionClient } from "./files.js";
|
|
7
9
|
import { isIgnoredPath } from "./ignore.js";
|
|
8
10
|
import { adoptLocal, saveState } from "./state.js";
|
|
9
11
|
export { fetchAgentsMd } from "./agentfiles.js";
|
|
@@ -86,10 +88,24 @@ export async function pullCommand(framingId, dir) {
|
|
|
86
88
|
// Always the editor: a play framing here would mirror the published copy and
|
|
87
89
|
// seed a state file that later syncs into a server wiped on every publish.
|
|
88
90
|
const { session, framingId: editFramingId } = await resolveEditSession(framingId, (err) => console.log(`${err.message} Mirroring the editor (${err.makeFramingId}) instead.`));
|
|
89
|
-
const client =
|
|
91
|
+
const client = editSessionClient(editFramingId, session);
|
|
90
92
|
console.log(`Session ready at ${sessionBaseUrl(session)}. Downloading files ...`);
|
|
91
93
|
const result = await pullTree(client, dir, editFramingId);
|
|
92
|
-
|
|
93
|
-
result.agentFiles = writeAgentFiles(dir, composeAgentsMd(platformDoc, engineDoc));
|
|
94
|
+
Object.assign(result, await refreshAgentFiles(client, dir));
|
|
94
95
|
return result;
|
|
95
96
|
}
|
|
97
|
+
/**
|
|
98
|
+
* The harness files that follow the world: AGENTS.md (the platform guide plus
|
|
99
|
+
* the world's own), the `./atomo` wrapper, and the `.claude/skills` copies of
|
|
100
|
+
* the platform's skill packages. Run after a pull, and by sync whenever the
|
|
101
|
+
* world's `Skills/` changes, which is how a skills install or withdrawal
|
|
102
|
+
* shows up.
|
|
103
|
+
*/
|
|
104
|
+
export async function refreshAgentFiles(client, dir) {
|
|
105
|
+
const [platformDoc, engineDoc] = await Promise.all([fetchAgentsMd(), fetchEngineAgentsMd(client)]);
|
|
106
|
+
const agentFiles = writeAgentFiles(dir, composeAgentsMd(platformDoc, engineDoc));
|
|
107
|
+
const wrapper = writeAtomoWrapper(dir);
|
|
108
|
+
if (wrapper)
|
|
109
|
+
agentFiles.written.push(wrapper);
|
|
110
|
+
return { agentFiles, skills: mirrorSkills(dir, skillsActive(engineDoc)) };
|
|
111
|
+
}
|
package/dist/skills.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The platform's agent skills, for a harness on the maker's machine.
|
|
3
|
+
*
|
|
4
|
+
* Inside a session the platform installs partner skill packages
|
|
5
|
+
* (`Skills/atomontage-*`) into the world and routes its agent to them from a
|
|
6
|
+
* managed block in the world's AGENTS.md. The packages reach a pulled folder
|
|
7
|
+
* like any other world file, but Claude Code discovers skills only under
|
|
8
|
+
* `.claude/skills/`, a dot-folder sync never mirrors. So the CLI copies each
|
|
9
|
+
* installed package there while the world's routing block is present, and
|
|
10
|
+
* removes its copies once the platform withdraws them (rollout 0, or an
|
|
11
|
+
* engine the packages do not support). The block is the signal, not the
|
|
12
|
+
* folders: those stay in the world after a withdrawal because games built
|
|
13
|
+
* from the starters attach their scripts by path.
|
|
14
|
+
*/
|
|
15
|
+
import fs from "node:fs";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
export const ROUTING_BEGIN = "<!-- BEGIN MANAGED ATOMONTAGE SKILLS ROUTING -->";
|
|
18
|
+
const PACKAGE_PREFIX = "atomontage-";
|
|
19
|
+
const MIRROR_ROOT = path.join(".claude", "skills");
|
|
20
|
+
/** Marks a `.claude/skills` entry as the CLI's copy; a maker's own skill of the same name is never touched. */
|
|
21
|
+
export const MIRROR_MARKER = ".virtualmatter-mirror";
|
|
22
|
+
/** True when the world's own AGENTS.md routes its agent to the packages. */
|
|
23
|
+
export function skillsActive(engineAgentsMd) {
|
|
24
|
+
return engineAgentsMd !== null && engineAgentsMd.includes(ROUTING_BEGIN);
|
|
25
|
+
}
|
|
26
|
+
/** The skill packages present in the folder's `Skills/`, sorted. */
|
|
27
|
+
export function installedPackages(dir) {
|
|
28
|
+
const root = path.join(dir, "Skills");
|
|
29
|
+
let names;
|
|
30
|
+
try {
|
|
31
|
+
names = fs.readdirSync(root);
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
return names
|
|
37
|
+
.filter((name) => name.startsWith(PACKAGE_PREFIX) && fs.existsSync(path.join(root, name, "SKILL.md")))
|
|
38
|
+
.sort();
|
|
39
|
+
}
|
|
40
|
+
/** Bring `.claude/skills/` in line with the world's packages; never touches an entry the CLI did not write. */
|
|
41
|
+
export function mirrorSkills(dir, active) {
|
|
42
|
+
const root = path.join(dir, MIRROR_ROOT);
|
|
43
|
+
const wanted = active ? installedPackages(dir) : [];
|
|
44
|
+
const ours = (name) => fs.existsSync(path.join(root, name, MIRROR_MARKER));
|
|
45
|
+
const result = { mirrored: [], removed: [] };
|
|
46
|
+
let existing = [];
|
|
47
|
+
try {
|
|
48
|
+
existing = fs.readdirSync(root);
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
/* nothing mirrored yet */
|
|
52
|
+
}
|
|
53
|
+
for (const name of existing) {
|
|
54
|
+
if (wanted.includes(name) || !ours(name))
|
|
55
|
+
continue;
|
|
56
|
+
fs.rmSync(path.join(root, name), { recursive: true, force: true });
|
|
57
|
+
result.removed.push(name);
|
|
58
|
+
}
|
|
59
|
+
for (const name of wanted) {
|
|
60
|
+
const target = path.join(root, name);
|
|
61
|
+
if (fs.existsSync(target) && !ours(name))
|
|
62
|
+
continue;
|
|
63
|
+
fs.rmSync(target, { recursive: true, force: true });
|
|
64
|
+
fs.cpSync(path.join(dir, "Skills", name), target, {
|
|
65
|
+
recursive: true,
|
|
66
|
+
// .zma files are the engine's asset records, not part of the skill
|
|
67
|
+
filter: (src) => !src.endsWith(".zma"),
|
|
68
|
+
});
|
|
69
|
+
fs.writeFileSync(path.join(target, MIRROR_MARKER), `Copied by the virtualmatter CLI from Skills/${name} and refreshed on pull and sync.\n`);
|
|
70
|
+
result.mirrored.push(name);
|
|
71
|
+
}
|
|
72
|
+
return result;
|
|
73
|
+
}
|