virtualmatter 0.6.5 → 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 +37 -6
- package/dist/agent-logs.js +33 -5
- package/dist/agents-md.generated.js +1 -1
- package/dist/files.js +200 -19
- package/dist/ignore.js +12 -0
- package/dist/index.js +222 -22
- package/dist/lock.js +208 -0
- package/dist/log-events.js +2 -2
- package/dist/mcp.js +137 -3
- package/dist/pull.js +2 -2
- package/dist/sync.js +305 -36
- 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
|
@@ -5,7 +5,7 @@ import { resolveEditSession, sessionBaseUrl } from "./api.js";
|
|
|
5
5
|
import { composeAgentsMd, fetchAgentsMd, writeAgentFiles } from "./agentfiles.js";
|
|
6
6
|
import { writeAtomoWrapper } from "./atomo.js";
|
|
7
7
|
import { mirrorSkills, skillsActive } from "./skills.js";
|
|
8
|
-
import {
|
|
8
|
+
import { editSessionClient } from "./files.js";
|
|
9
9
|
import { isIgnoredPath } from "./ignore.js";
|
|
10
10
|
import { adoptLocal, saveState } from "./state.js";
|
|
11
11
|
export { fetchAgentsMd } from "./agentfiles.js";
|
|
@@ -88,7 +88,7 @@ export async function pullCommand(framingId, dir) {
|
|
|
88
88
|
// Always the editor: a play framing here would mirror the published copy and
|
|
89
89
|
// seed a state file that later syncs into a server wiped on every publish.
|
|
90
90
|
const { session, framingId: editFramingId } = await resolveEditSession(framingId, (err) => console.log(`${err.message} Mirroring the editor (${err.makeFramingId}) instead.`));
|
|
91
|
-
const client =
|
|
91
|
+
const client = editSessionClient(editFramingId, session);
|
|
92
92
|
console.log(`Session ready at ${sessionBaseUrl(session)}. Downloading files ...`);
|
|
93
93
|
const result = await pullTree(client, dir, editFramingId);
|
|
94
94
|
Object.assign(result, await refreshAgentFiles(client, dir));
|