rpg-engine 0.21.0 → 0.23.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 +5 -5
- package/dist/campaign/create-campaign.d.ts +5 -0
- package/dist/campaign/create-campaign.js +40 -12
- package/dist/campaign/find-campaign.d.ts +5 -0
- package/dist/campaign/find-campaign.js +10 -5
- package/dist/campaign/host-files.d.ts +15 -4
- package/dist/campaign/host-files.js +134 -14
- package/dist/campaign/index.d.ts +4 -3
- package/dist/campaign/index.js +4 -3
- package/dist/campaign/port.d.ts +6 -0
- package/dist/campaign/port.js +24 -0
- package/dist/campaign/settings.d.ts +17 -0
- package/dist/campaign/settings.js +24 -1
- package/dist/cli/cli.js +33 -6
- package/dist/commands/config/config.js +15 -1
- package/dist/commands/config/dashboard.d.ts +4 -0
- package/dist/commands/config/dashboard.js +43 -7
- package/dist/commands/config/shown.js +4 -0
- package/dist/commands/config/words.d.ts +3 -1
- package/dist/commands/config/words.js +29 -2
- package/dist/commands/image.d.ts +3 -0
- package/dist/commands/image.js +31 -4
- package/dist/commands/import/import.d.ts +1 -1
- package/dist/commands/import/import.js +5 -0
- package/dist/commands/init.d.ts +2 -0
- package/dist/commands/init.js +21 -3
- package/dist/commands/mode.d.ts +1 -1
- package/dist/commands/package/data.js +27 -0
- package/dist/commands/package/theme.js +2 -1
- package/dist/commands/scene/scene.js +6 -1
- package/dist/commands/shared/books/adventures.d.ts +1 -0
- package/dist/commands/shared/books/adventures.js +1 -1
- package/dist/commands/shared/campaign/git.d.ts +25 -0
- package/dist/commands/shared/campaign/git.js +104 -0
- package/dist/commands/shared/campaign/images.js +5 -3
- package/dist/commands/shared/campaign/saves.js +5 -1
- package/dist/commands/shared/characters/voices.d.ts +2 -2
- package/dist/commands/shared/characters/voices.js +8 -5
- package/dist/commands/shared/packages/catalog.js +24 -5
- package/dist/commands/status.d.ts +1 -0
- package/dist/commands/status.js +8 -4
- package/dist/core/context.d.ts +17 -6
- package/dist/core/context.js +2 -1
- package/dist/core/version.d.ts +2 -0
- package/dist/core/version.js +5 -0
- package/dist/dashboard/guide/ar.md +12 -7
- package/dist/dashboard/guide/cs.md +11 -6
- package/dist/dashboard/guide/da.md +11 -6
- package/dist/dashboard/guide/de.md +13 -8
- package/dist/dashboard/guide/el.md +11 -6
- package/dist/dashboard/guide/en.md +10 -5
- package/dist/dashboard/guide/es.md +11 -6
- package/dist/dashboard/guide/fi.md +11 -6
- package/dist/dashboard/guide/fr.md +12 -7
- package/dist/dashboard/guide/he.md +11 -6
- package/dist/dashboard/guide/hi.md +11 -6
- package/dist/dashboard/guide/hu.md +11 -6
- package/dist/dashboard/guide/is.md +11 -6
- package/dist/dashboard/guide/it.md +42 -37
- package/dist/dashboard/guide/ja.md +12 -7
- package/dist/dashboard/guide/ko.md +11 -6
- package/dist/dashboard/guide/nb.md +12 -7
- package/dist/dashboard/guide/nl.md +13 -8
- package/dist/dashboard/guide/pl.md +11 -6
- package/dist/dashboard/guide/pt.md +11 -6
- package/dist/dashboard/guide/ro.md +11 -6
- package/dist/dashboard/guide/ru.md +12 -7
- package/dist/dashboard/guide/sv.md +11 -6
- package/dist/dashboard/guide/tr.md +12 -7
- package/dist/dashboard/guide/uk.md +12 -7
- package/dist/dashboard/guide/zh.md +11 -6
- package/dist/dashboard/i18n/ar.json +30 -2
- package/dist/dashboard/i18n/cs.json +30 -2
- package/dist/dashboard/i18n/da.json +30 -2
- package/dist/dashboard/i18n/de.json +30 -2
- package/dist/dashboard/i18n/el.json +30 -2
- package/dist/dashboard/i18n/es.json +30 -2
- package/dist/dashboard/i18n/fi.json +30 -2
- package/dist/dashboard/i18n/fr.json +30 -2
- package/dist/dashboard/i18n/he.json +30 -2
- package/dist/dashboard/i18n/hi.json +30 -2
- package/dist/dashboard/i18n/hu.json +30 -2
- package/dist/dashboard/i18n/is.json +30 -2
- package/dist/dashboard/i18n/it.json +30 -2
- package/dist/dashboard/i18n/ja.json +30 -2
- package/dist/dashboard/i18n/ko.json +30 -2
- package/dist/dashboard/i18n/nb.json +30 -2
- package/dist/dashboard/i18n/nl.json +30 -2
- package/dist/dashboard/i18n/pl.json +30 -2
- package/dist/dashboard/i18n/pt.json +30 -2
- package/dist/dashboard/i18n/ro.json +30 -2
- package/dist/dashboard/i18n/ru.json +30 -2
- package/dist/dashboard/i18n/sv.json +30 -2
- package/dist/dashboard/i18n/tr.json +30 -2
- package/dist/dashboard/i18n/uk.json +30 -2
- package/dist/dashboard/i18n/zh.json +30 -2
- package/dist/dashboard/packages/installed.js +2 -2
- package/dist/dashboard/packages/packages.js +1 -1
- package/dist/dashboard/parts/labels.js +7 -0
- package/dist/dashboard/settings/settings.js +45 -9
- package/dist/dashboard/shell/notices.js +1 -1
- package/dist/dashboard/shell/system-style.js +89 -5
- package/dist/dashboard/shell/tooltip.js +5 -4
- package/dist/dashboard/style.css +245 -84
- package/dist/git/git.d.ts +12 -0
- package/dist/git/git.js +175 -0
- package/dist/git/ignore.d.ts +2 -0
- package/dist/git/ignore.js +16 -0
- package/dist/git/index.d.ts +2 -0
- package/dist/git/index.js +2 -0
- package/dist/helpers/briefs.d.ts +4 -0
- package/dist/helpers/briefs.js +13 -0
- package/dist/helpers/chosen.d.ts +8 -4
- package/dist/helpers/chosen.js +38 -14
- package/dist/helpers/index.d.ts +3 -2
- package/dist/helpers/index.js +5 -3
- package/dist/helpers/opencode.d.ts +13 -0
- package/dist/helpers/opencode.js +202 -0
- package/dist/importer/index.d.ts +1 -1
- package/dist/importer/index.js +1 -1
- package/dist/instructions/index.d.ts +2 -2
- package/dist/instructions/index.js +3 -3
- package/dist/languages/pack.js +2 -1
- package/dist/server/approver.d.ts +4 -2
- package/dist/server/approver.js +27 -7
- package/dist/server/bell/antigravity.d.ts +10 -0
- package/dist/server/bell/antigravity.js +95 -0
- package/dist/server/bell/index.d.ts +2 -0
- package/dist/server/bell/index.js +2 -0
- package/dist/server/bell/listener.d.ts +43 -0
- package/dist/server/bell/listener.js +149 -0
- package/dist/server/dashboard/files.d.ts +3 -2
- package/dist/server/dashboard/files.js +44 -15
- package/dist/server/dashboard/index.d.ts +25 -3
- package/dist/server/dashboard/index.js +58 -23
- package/dist/server/dashboard/news.d.ts +5 -2
- package/dist/server/dashboard/news.js +82 -14
- package/dist/server/dashboard/ports.d.ts +17 -2
- package/dist/server/dashboard/ports.js +60 -4
- package/dist/server/mcp.d.ts +6 -4
- package/dist/server/mcp.js +66 -18
- package/dist/server/player-acts.d.ts +21 -1
- package/dist/server/player-acts.js +38 -2
- package/dist/server/route/front.d.ts +3 -0
- package/dist/server/route/front.js +97 -0
- package/dist/server/route/index.d.ts +3 -0
- package/dist/server/route/index.js +3 -0
- package/dist/server/route/notes.d.ts +8 -0
- package/dist/server/route/notes.js +50 -0
- package/dist/server/route/router.d.ts +31 -0
- package/dist/server/route/router.js +215 -0
- package/dist/themes/css.d.ts +1 -1
- package/dist/themes/css.js +101 -2
- package/dist/themes/index.d.ts +1 -1
- package/dist/themes/index.js +1 -1
- package/dist/themes/theme.d.ts +34 -3
- package/dist/themes/theme.js +176 -19
- package/instructions/core.md +1 -0
- package/instructions/dashboard-guide.md +1 -1
- package/instructions/start.md +1 -1
- package/instructions/voices-opencode.md +1 -0
- package/package.json +3 -2
- package/plugins/opencode.js +174 -0
- package/dist/server/bell-listener.d.ts +0 -9
- package/dist/server/bell-listener.js +0 -83
|
@@ -5,10 +5,16 @@ export interface Act {
|
|
|
5
5
|
/** The thing of the sheet it starts from, by its page, whose known text the master hears with it (T-218). */
|
|
6
6
|
item?: string;
|
|
7
7
|
rung?: boolean;
|
|
8
|
+
/** Held for a hook that speaks it as the player: whether the hook said it took it (D-578). */
|
|
9
|
+
taken?: boolean;
|
|
8
10
|
}
|
|
9
|
-
/**
|
|
11
|
+
/**
|
|
12
|
+
* The sentences said and not yet heard, in order; those given to a hook that speaks them as the player, held until the master's next call begins (D-578);
|
|
13
|
+
* and the ids of those heard lately, for the page to tell.
|
|
14
|
+
*/
|
|
10
15
|
export interface Acts {
|
|
11
16
|
pending: Act[];
|
|
17
|
+
held: Act[];
|
|
12
18
|
heard: string[];
|
|
13
19
|
}
|
|
14
20
|
export declare function newActs(): Acts;
|
|
@@ -20,6 +26,20 @@ export declare function withdrawAct(acts: Acts, id: string): "withdrawn" | "hear
|
|
|
20
26
|
export declare function markRung(acts: Acts): void;
|
|
21
27
|
/** Marks sentences heard, as a call or Codex's queue took them. */
|
|
22
28
|
export declare function markHeard(acts: Acts, ids: string[]): void;
|
|
29
|
+
/** Holds the sentences waiting, as given to a hook that speaks them as the player, until it says it took them, then until the master's next call begins. */
|
|
30
|
+
export declare function holdActs(acts: Acts): Act[];
|
|
31
|
+
/** Marks held sentences as taken by the hook they were given to: they are on their way to the chat. */
|
|
32
|
+
export declare function takeHeld(acts: Acts, ids: string[]): void;
|
|
33
|
+
/**
|
|
34
|
+
* As a call of the master begins: the held sentences their hook took are heard; those it never took, as from a hook killed meanwhile, go back to the head of
|
|
35
|
+
* those waiting, for this call to take.
|
|
36
|
+
*/
|
|
37
|
+
export declare function hearHeld(acts: Acts): void;
|
|
38
|
+
/**
|
|
39
|
+
* Gives the held sentences among `ids` that the hook never took back to the head of those waiting, as the master never began a call after them; whether any
|
|
40
|
+
* came back. One the hook took is in the chat already, however long the master takes to call: given back, it would be said twice.
|
|
41
|
+
*/
|
|
42
|
+
export declare function giveBackHeld(acts: Acts, ids: string[]): boolean;
|
|
23
43
|
/** Gives a sentence back to the head of those waiting, as one Codex never took. */
|
|
24
44
|
export declare function giveBackAct(acts: Acts, act: Act): void;
|
|
25
45
|
/**
|
|
@@ -5,7 +5,7 @@ const heardKept = 20;
|
|
|
5
5
|
// What does not show, or breaks a line in the player's words, from a single sentence: control characters, which no process could take either.
|
|
6
6
|
const unseen = /[\u0000-\u001f\u007f]+/g;
|
|
7
7
|
export function newActs() {
|
|
8
|
-
return { pending: [], heard: [] };
|
|
8
|
+
return { pending: [], held: [], heard: [] };
|
|
9
9
|
}
|
|
10
10
|
/** The player's words as one sentence: what does not show taken away, every break a space; empty when nothing is left. */
|
|
11
11
|
export function actText(text) {
|
|
@@ -15,7 +15,7 @@ export function actText(text) {
|
|
|
15
15
|
export function withdrawAct(acts, id) {
|
|
16
16
|
const index = acts.pending.findIndex((act) => act.id === id);
|
|
17
17
|
if (index === -1) {
|
|
18
|
-
return acts.heard.includes(id) ? "heard" : "unknown";
|
|
18
|
+
return acts.heard.includes(id) || acts.held.some((act) => act.id === id) ? "heard" : "unknown";
|
|
19
19
|
}
|
|
20
20
|
// The doorbell rang for it: the master is called, and would find nothing.
|
|
21
21
|
if (acts.pending[index].rung === true) {
|
|
@@ -33,6 +33,42 @@ export function markHeard(acts, ids) {
|
|
|
33
33
|
acts.pending = acts.pending.filter((act) => !ids.includes(act.id));
|
|
34
34
|
acts.heard = [...acts.heard.filter((id) => !ids.includes(id)), ...ids].slice(-heardKept);
|
|
35
35
|
}
|
|
36
|
+
/** Holds the sentences waiting, as given to a hook that speaks them as the player, until it says it took them, then until the master's next call begins. */
|
|
37
|
+
export function holdActs(acts) {
|
|
38
|
+
const taken = acts.pending;
|
|
39
|
+
acts.pending = [];
|
|
40
|
+
acts.held = [...acts.held, ...taken.map((act) => ({ ...act, rung: true, taken: false }))];
|
|
41
|
+
return taken;
|
|
42
|
+
}
|
|
43
|
+
/** Marks held sentences as taken by the hook they were given to: they are on their way to the chat. */
|
|
44
|
+
export function takeHeld(acts, ids) {
|
|
45
|
+
acts.held = acts.held.map((act) => (ids.includes(act.id) ? { ...act, taken: true } : act));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* As a call of the master begins: the held sentences their hook took are heard; those it never took, as from a hook killed meanwhile, go back to the head of
|
|
49
|
+
* those waiting, for this call to take.
|
|
50
|
+
*/
|
|
51
|
+
export function hearHeld(acts) {
|
|
52
|
+
const held = acts.held;
|
|
53
|
+
acts.held = [];
|
|
54
|
+
markHeard(acts, held.filter((act) => act.taken === true).map((act) => act.id));
|
|
55
|
+
acts.pending = [...held.filter((act) => act.taken !== true).map(waiting), ...acts.pending];
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Gives the held sentences among `ids` that the hook never took back to the head of those waiting, as the master never began a call after them; whether any
|
|
59
|
+
* came back. One the hook took is in the chat already, however long the master takes to call: given back, it would be said twice.
|
|
60
|
+
*/
|
|
61
|
+
export function giveBackHeld(acts, ids) {
|
|
62
|
+
const back = acts.held.filter((act) => ids.includes(act.id) && act.taken !== true);
|
|
63
|
+
acts.held = acts.held.filter((act) => !back.includes(act));
|
|
64
|
+
acts.pending = [...back.map(waiting), ...acts.pending];
|
|
65
|
+
return back.length > 0;
|
|
66
|
+
}
|
|
67
|
+
/** A held sentence as one waiting again, neither rung for nor taken. */
|
|
68
|
+
function waiting(act) {
|
|
69
|
+
const { rung: _, taken: __, ...rest } = act;
|
|
70
|
+
return rest;
|
|
71
|
+
}
|
|
36
72
|
/** Gives a sentence back to the head of those waiting, as one Codex never took. */
|
|
37
73
|
export function giveBackAct(acts, act) {
|
|
38
74
|
acts.pending.unshift(act);
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
// Where `rpg mcp` reads its client (D-578): in a campaign, straight from its input, as always; started outside one, as Antigravity's app does, which
|
|
2
|
+
// starts one server for all its folders, it becomes the router, with a child `rpg mcp` for each campaign a call plays and one in its own folder.
|
|
3
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
4
|
+
import { createInterface } from "node:readline";
|
|
5
|
+
import { findCampaign } from "../../campaign/index.js";
|
|
6
|
+
import { userDataFolder } from "../../core/context.js";
|
|
7
|
+
import { notedCampaign, notesFolder, pruneNotes } from "./notes.js";
|
|
8
|
+
import { mcpRouter } from "./router.js";
|
|
9
|
+
// Set for the router's children, which serve as any server does and never route.
|
|
10
|
+
const routedMark = "RPG_ROUTED";
|
|
11
|
+
/** The input the MCP server reads; undefined when the router took it, and serves until the client goes. */
|
|
12
|
+
export function serverInput(folder = process.cwd(), environment = process.env) {
|
|
13
|
+
if (environment[routedMark] === "1" || findCampaign(folder) !== undefined) {
|
|
14
|
+
return process.stdin;
|
|
15
|
+
}
|
|
16
|
+
// Outside a campaign every client gets the router, which passes the lines through to its own folder's child until a call names a conversation: Antigravity's
|
|
17
|
+
// app speaks before its initialize, so its name comes too late to choose by.
|
|
18
|
+
route(process.stdin, folder);
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
/** The router at work on the process's input and output, its children `rpg mcp` started as this one was; what it does goes to stderr. */
|
|
22
|
+
function route(input, folder) {
|
|
23
|
+
const log = (line) => process.stderr.write(`${line}\n`);
|
|
24
|
+
const home = userDataFolder();
|
|
25
|
+
pruneNotes(home, new Date());
|
|
26
|
+
const router = mcpRouter({
|
|
27
|
+
folder,
|
|
28
|
+
start: startChild,
|
|
29
|
+
campaignOf: (conversation) => notedCampaign(home, conversation),
|
|
30
|
+
toClient: (line) => process.stdout.write(`${line}\n`),
|
|
31
|
+
log,
|
|
32
|
+
});
|
|
33
|
+
log(`rpg mcp router: serving from ${folder}, outside any campaign; the campaigns of the app's conversations are noted in ${notesFolder(home)}`);
|
|
34
|
+
const lines = createInterface({ input, crlfDelay: Infinity });
|
|
35
|
+
lines.on("line", (line) => void router.fromClient(line));
|
|
36
|
+
// The client is gone: every child ends with it.
|
|
37
|
+
lines.on("close", () => {
|
|
38
|
+
router.stop();
|
|
39
|
+
process.exit(0);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
/** A child `rpg mcp` in a folder, as this process was started; its end, as when its folder is gone, is told to the router. */
|
|
43
|
+
function startChild(folder) {
|
|
44
|
+
const child = spawn(process.execPath, [...process.execArgv, process.argv[1] ?? "", "mcp"], {
|
|
45
|
+
cwd: folder,
|
|
46
|
+
env: { ...process.env, [routedMark]: "1" },
|
|
47
|
+
stdio: ["pipe", "pipe", "inherit"],
|
|
48
|
+
windowsHide: true,
|
|
49
|
+
detached: process.platform !== "win32",
|
|
50
|
+
});
|
|
51
|
+
let endListener = () => undefined;
|
|
52
|
+
let stopping = false;
|
|
53
|
+
const end = (reason) => {
|
|
54
|
+
if (!stopping) {
|
|
55
|
+
stopping = true;
|
|
56
|
+
endListener(reason);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
child.stdin.on("error", () => undefined);
|
|
60
|
+
child.on("error", (error) => end(error.message));
|
|
61
|
+
child.on("exit", (code, signal) => end(signal ?? `exit code ${String(code)}`));
|
|
62
|
+
return {
|
|
63
|
+
write: (line) => {
|
|
64
|
+
if (child.stdin.writable) {
|
|
65
|
+
child.stdin.write(`${line}\n`);
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
onLine: (listener) => void createInterface({ input: child.stdout, crlfDelay: Infinity }).on("line", listener),
|
|
69
|
+
onEnd: (listener) => (endListener = listener),
|
|
70
|
+
stop: () => {
|
|
71
|
+
stopping = true;
|
|
72
|
+
endTree(child.pid, () => child.kill());
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Ends a process with what it started: on Windows its whole tree, else its process group; the process alone when neither can be. */
|
|
77
|
+
function endTree(pid, alone) {
|
|
78
|
+
try {
|
|
79
|
+
if (pid === undefined) {
|
|
80
|
+
alone();
|
|
81
|
+
}
|
|
82
|
+
else if (process.platform === "win32") {
|
|
83
|
+
spawnSync("taskkill", ["/T", "/F", "/PID", String(pid)], { windowsHide: true, stdio: "ignore" });
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
process.kill(-pid, "SIGTERM");
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch {
|
|
90
|
+
try {
|
|
91
|
+
alone();
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// Gone already.
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** Notes that the conversation plays the campaign at `root`; a note that cannot be written leaves the conversation to the server's own folder. */
|
|
2
|
+
export declare function noteConversation(home: string, conversation: string, root: string): void;
|
|
3
|
+
/** The campaign a conversation plays, as its note says; undefined without one, or once the folder is no campaign. */
|
|
4
|
+
export declare function notedCampaign(home: string, conversation: string): string | undefined;
|
|
5
|
+
/** Takes away the notes not written for a month, as of `now`. */
|
|
6
|
+
export declare function pruneNotes(home: string, now: Date): void;
|
|
7
|
+
/** The folder of the notes, in the engine's data folder. */
|
|
8
|
+
export declare function notesFolder(home: string): string;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Which campaign each conversation of Antigravity's app plays: its hook, run in the campaign's folder, notes it as it approves an rpg tool, and the router
|
|
2
|
+
// reads it, since the app starts one rpg server for all its folders and names only the conversation (D-578). A small file per conversation, in the engine's
|
|
3
|
+
// data folder on this computer, never in a campaign; those not written for a month go as the router starts.
|
|
4
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { campaignFileName } from "../../campaign/index.js";
|
|
7
|
+
// How long a note is kept after it was last written: each call of an rpg tool writes it again.
|
|
8
|
+
const keptFor = 30 * 24 * 60 * 60 * 1000;
|
|
9
|
+
/** Notes that the conversation plays the campaign at `root`; a note that cannot be written leaves the conversation to the server's own folder. */
|
|
10
|
+
export function noteConversation(home, conversation, root) {
|
|
11
|
+
try {
|
|
12
|
+
mkdirSync(notesFolder(home), { recursive: true });
|
|
13
|
+
writeFileSync(notePath(home, conversation), `${root}\n`);
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
// The game goes on where the server runs.
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/** The campaign a conversation plays, as its note says; undefined without one, or once the folder is no campaign. */
|
|
20
|
+
export function notedCampaign(home, conversation) {
|
|
21
|
+
try {
|
|
22
|
+
const root = readFileSync(notePath(home, conversation), "utf8").trim();
|
|
23
|
+
return root !== "" && existsSync(join(root, campaignFileName)) ? root : undefined;
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Takes away the notes not written for a month, as of `now`. */
|
|
30
|
+
export function pruneNotes(home, now) {
|
|
31
|
+
try {
|
|
32
|
+
for (const name of readdirSync(notesFolder(home))) {
|
|
33
|
+
const path = join(notesFolder(home), name);
|
|
34
|
+
if (now.getTime() - statSync(path).mtimeMs > keptFor) {
|
|
35
|
+
rmSync(path, { force: true });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
catch {
|
|
40
|
+
// No notes yet, or none that can go: they stay.
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** The folder of the notes, in the engine's data folder. */
|
|
44
|
+
export function notesFolder(home) {
|
|
45
|
+
return join(home, "antigravity-conversations");
|
|
46
|
+
}
|
|
47
|
+
// A conversation's id as a file name: only letters, digits, dashes and underscores.
|
|
48
|
+
function notePath(home, conversation) {
|
|
49
|
+
return join(notesFolder(home), `${conversation.replace(/[^\w-]/g, "_").slice(0, 120)}.txt`);
|
|
50
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** A child `rpg mcp` as the router drives it: the lines it is given, the lines it writes, its end, as when it exits or its folder is gone, and stopping it. */
|
|
2
|
+
export interface ChildServer {
|
|
3
|
+
write(line: string): void;
|
|
4
|
+
onLine(listener: (line: string) => void): void;
|
|
5
|
+
onEnd(listener: (reason: string) => void): void;
|
|
6
|
+
stop(): void;
|
|
7
|
+
}
|
|
8
|
+
export interface RouterOptions {
|
|
9
|
+
/** The folder the server was started in, where the child for every call without a noted campaign runs. */
|
|
10
|
+
folder: string;
|
|
11
|
+
/** Starts a child `rpg mcp` in a folder. */
|
|
12
|
+
start: (folder: string) => ChildServer;
|
|
13
|
+
/** The campaign a conversation plays, as noted; undefined until noted. */
|
|
14
|
+
campaignOf: (conversation: string) => string | undefined;
|
|
15
|
+
/** Writes a line to the client. */
|
|
16
|
+
toClient: (line: string) => void;
|
|
17
|
+
/** How long a conversation's first call waits for its note, which may come a moment after it: so many looks, so many ms apart. */
|
|
18
|
+
noteLooks?: {
|
|
19
|
+
every: number;
|
|
20
|
+
times: number;
|
|
21
|
+
};
|
|
22
|
+
/** A line about what the router does, for the player who looks at its stderr. */
|
|
23
|
+
log?: (line: string) => void;
|
|
24
|
+
}
|
|
25
|
+
/** The client's meta key that names Antigravity's conversation in every call. */
|
|
26
|
+
export declare const conversationKey = "antigravity.google/conversation_id";
|
|
27
|
+
/** A router between the client and its children: give it each line the client writes, in order; `stop` ends every child, as the client goes. */
|
|
28
|
+
export declare function mcpRouter(options: RouterOptions): {
|
|
29
|
+
fromClient(line: string): Promise<void>;
|
|
30
|
+
stop(): void;
|
|
31
|
+
};
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
// The router of `rpg mcp` started outside a campaign, as Antigravity's app starts it (D-578): one server for all its folders, which names in each call only the
|
|
2
|
+
// conversation. It keeps one child `rpg mcp` for each campaign a call plays, started the first time it is needed with the client's handshake, and one in its own
|
|
3
|
+
// folder for everything else: each call goes to the child of its conversation's noted campaign, the others to its own folder's, never to another campaign's.
|
|
4
|
+
import { isAbsolute } from "node:path";
|
|
5
|
+
import { samePath } from "../../library/index.js";
|
|
6
|
+
/** The client's meta key that names Antigravity's conversation in every call. */
|
|
7
|
+
export const conversationKey = "antigravity.google/conversation_id";
|
|
8
|
+
/** A router between the client and its children: give it each line the client writes, in order; `stop` ends every child, as the client goes. */
|
|
9
|
+
export function mcpRouter(options) {
|
|
10
|
+
const looks = options.noteLooks ?? { every: 200, times: 10 };
|
|
11
|
+
const log = options.log ?? (() => undefined);
|
|
12
|
+
const children = [];
|
|
13
|
+
let handshake = {};
|
|
14
|
+
// The child that answered the client's last tools/list, and the one its last request went to.
|
|
15
|
+
let listedBy;
|
|
16
|
+
let current;
|
|
17
|
+
// The children's own requests to the client, by the id the router gave them.
|
|
18
|
+
const asked = new Map();
|
|
19
|
+
let askedCount = 0;
|
|
20
|
+
// The conversations whose note was looked for with patience already: later calls look once.
|
|
21
|
+
const waited = new Set();
|
|
22
|
+
let queue = Promise.resolve();
|
|
23
|
+
const childIn = (folder) => {
|
|
24
|
+
const found = children.find((child) => samePath(child.folder, folder));
|
|
25
|
+
return found ?? startChild(folder);
|
|
26
|
+
};
|
|
27
|
+
const startChild = (folder) => {
|
|
28
|
+
const child = { folder, pending: new Map(), server: options.start(folder) };
|
|
29
|
+
children.push(child);
|
|
30
|
+
child.server.onLine((line) => fromChild(child, line));
|
|
31
|
+
child.server.onEnd((reason) => ended(child, reason));
|
|
32
|
+
// A child started after the handshake gets it again; its answer to initialize is its own, the client had one.
|
|
33
|
+
if (handshake.initialize !== undefined) {
|
|
34
|
+
child.replayed = parsed(handshake.initialize)?.id;
|
|
35
|
+
child.server.write(handshake.initialize);
|
|
36
|
+
}
|
|
37
|
+
if (handshake.initialized !== undefined) {
|
|
38
|
+
child.server.write(handshake.initialized);
|
|
39
|
+
}
|
|
40
|
+
return child;
|
|
41
|
+
};
|
|
42
|
+
const fromChild = (child, line) => {
|
|
43
|
+
const message = parsed(line);
|
|
44
|
+
if (message === undefined || !children.includes(child)) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (message.method === undefined && message.id !== undefined) {
|
|
48
|
+
if (child.replayed !== undefined && message.id === child.replayed) {
|
|
49
|
+
child.replayed = undefined;
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const request = child.pending.get(message.id);
|
|
53
|
+
child.pending.delete(message.id);
|
|
54
|
+
if (request?.method === "tools/list") {
|
|
55
|
+
listedBy = child;
|
|
56
|
+
}
|
|
57
|
+
options.toClient(request?.advice === undefined ? line : JSON.stringify(withAdvice(message, request.advice)));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
// A request of the child's own to the client, as a form: an id no other child's can take.
|
|
61
|
+
if (message.method !== undefined && message.id !== undefined) {
|
|
62
|
+
askedCount++;
|
|
63
|
+
const id = `rpg-route-${askedCount}`;
|
|
64
|
+
asked.set(id, { child, id: message.id });
|
|
65
|
+
options.toClient(JSON.stringify({ ...message, id }));
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
// The child takes back a request of its own: the client knows it by the id the router gave it.
|
|
69
|
+
if (message.method === "notifications/cancelled") {
|
|
70
|
+
const requestId = message.params?.requestId;
|
|
71
|
+
const given = [...asked].find(([, request]) => request.child === child && request.id === requestId)?.[0];
|
|
72
|
+
if (given !== undefined) {
|
|
73
|
+
options.toClient(JSON.stringify({ ...message, params: { ...message.params, requestId: given } }));
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
options.toClient(line);
|
|
78
|
+
};
|
|
79
|
+
// A child that ends is forgotten, and the client's requests it held are answered: the next call for its folder starts another.
|
|
80
|
+
const ended = (child, reason) => {
|
|
81
|
+
const index = children.indexOf(child);
|
|
82
|
+
if (index === -1) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
children.splice(index, 1);
|
|
86
|
+
listedBy = listedBy === child ? undefined : listedBy;
|
|
87
|
+
current = current === child ? undefined : current;
|
|
88
|
+
log(`rpg mcp router: the server in ${child.folder} ended (${reason})`);
|
|
89
|
+
for (const [id] of child.pending) {
|
|
90
|
+
options.toClient(JSON.stringify({ jsonrpc: "2.0", id, error: { code: -32603, message: `The rpg server in ${child.folder} stopped (${reason}): call again.` } }));
|
|
91
|
+
}
|
|
92
|
+
for (const [id, request] of asked) {
|
|
93
|
+
if (request.child === child) {
|
|
94
|
+
asked.delete(id);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const route = async (line) => {
|
|
99
|
+
const message = parsed(line);
|
|
100
|
+
if (message === undefined) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
// An answer of the client's to a child's request goes back to that child, with the id it gave.
|
|
104
|
+
if (message.method === undefined) {
|
|
105
|
+
const request = typeof message.id === "string" ? asked.get(message.id) : undefined;
|
|
106
|
+
if (request !== undefined) {
|
|
107
|
+
asked.delete(message.id);
|
|
108
|
+
request.child.server.write(JSON.stringify({ ...message, id: request.id }));
|
|
109
|
+
}
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
if (message.method === "initialize") {
|
|
113
|
+
handshake = { initialize: line };
|
|
114
|
+
}
|
|
115
|
+
else if (message.method === "notifications/initialized") {
|
|
116
|
+
handshake.initialized = line;
|
|
117
|
+
}
|
|
118
|
+
if (message.id === undefined) {
|
|
119
|
+
notify(message, line);
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
const request = { method: String(message.method) };
|
|
123
|
+
const child = message.method === "tools/call" ? await callChild(message, request) : (current ?? childIn(options.folder));
|
|
124
|
+
if (child === undefined) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
child.pending.set(message.id, request);
|
|
128
|
+
child.server.write(line);
|
|
129
|
+
};
|
|
130
|
+
// A notification of the client's: one that cancels a request goes to the child that has it; the handshake's to the children there are; any other to all.
|
|
131
|
+
const notify = (message, line) => {
|
|
132
|
+
const cancelled = message.params?.requestId;
|
|
133
|
+
const holder = cancelled === undefined ? undefined : children.find((child) => child.pending.has(cancelled));
|
|
134
|
+
const targets = holder !== undefined ? [holder] : children.length > 0 ? children : [childIn(options.folder)];
|
|
135
|
+
targets.forEach((child) => child.server.write(line));
|
|
136
|
+
};
|
|
137
|
+
// The child a call goes to: its conversation's campaign as noted, else the router's own folder; a call there of a conversation the hooks never noted gets
|
|
138
|
+
// the advice it needs, or is answered at once when it would make a campaign in the app's own folder.
|
|
139
|
+
const callChild = async (message, request) => {
|
|
140
|
+
const conversation = conversationOf(message);
|
|
141
|
+
const root = conversation === undefined ? undefined : await noted(conversation);
|
|
142
|
+
const child = childIn(root ?? options.folder);
|
|
143
|
+
if (conversation !== undefined && root === undefined) {
|
|
144
|
+
const call = message.params;
|
|
145
|
+
const folder = call?.arguments?.folder;
|
|
146
|
+
if (call?.name === "init" && (typeof folder !== "string" || !isAbsolute(folder))) {
|
|
147
|
+
log(`rpg mcp router: call ${String(message.id)} of conversation ${conversation} asks init with no full path: answered without a server`);
|
|
148
|
+
options.toClient(JSON.stringify({ jsonrpc: "2.0", id: message.id, result: { content: [{ type: "text", text: initWhere }], isError: true } }));
|
|
149
|
+
return undefined;
|
|
150
|
+
}
|
|
151
|
+
request.advice = call?.name === "init" ? openThere(folder) : openThere();
|
|
152
|
+
}
|
|
153
|
+
log(`rpg mcp router: call ${String(message.id)}${conversation === undefined ? "" : ` of conversation ${conversation}`} goes to the server in ${child.folder}${root === undefined && conversation !== undefined ? " (no campaign noted for it)" : ""}`);
|
|
154
|
+
// The client lists its tools from one child: one whose tools it did not list may have others, as another campaign's packages give.
|
|
155
|
+
if (listedBy !== undefined && listedBy !== child) {
|
|
156
|
+
options.toClient(JSON.stringify({ jsonrpc: "2.0", method: "notifications/tools/list_changed" }));
|
|
157
|
+
listedBy = undefined;
|
|
158
|
+
}
|
|
159
|
+
current = child;
|
|
160
|
+
return child;
|
|
161
|
+
};
|
|
162
|
+
const noted = async (conversation) => {
|
|
163
|
+
let root = options.campaignOf(conversation);
|
|
164
|
+
for (let look = 1; root === undefined && !waited.has(conversation) && look <= looks.times; look++) {
|
|
165
|
+
await new Promise((done) => setTimeout(done, looks.every));
|
|
166
|
+
root = options.campaignOf(conversation);
|
|
167
|
+
}
|
|
168
|
+
waited.add(conversation);
|
|
169
|
+
return root;
|
|
170
|
+
};
|
|
171
|
+
// The child in the router's own folder serves from the start, as the server alone would.
|
|
172
|
+
childIn(options.folder);
|
|
173
|
+
return {
|
|
174
|
+
// One line at a time, in order: a call that waits for its note holds those after it.
|
|
175
|
+
fromClient: (line) => {
|
|
176
|
+
queue = queue.then(() => route(line)).catch(() => undefined);
|
|
177
|
+
return queue;
|
|
178
|
+
},
|
|
179
|
+
stop: () => {
|
|
180
|
+
for (const child of children.splice(0)) {
|
|
181
|
+
child.server.stop();
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
// What init answers, called from the app's own folder with no full path: it would make a campaign among the app's files.
|
|
187
|
+
const initWhere = "This chat runs in no campaign's folder, so init needs where to make the campaign: ask the player which folder, and pass its full path as folder.";
|
|
188
|
+
/** What the master is told after a call from the app's own folder: the chat must start in the campaign's folder, where its hooks load. */
|
|
189
|
+
function openThere(folder) {
|
|
190
|
+
const where = folder === undefined ? "the campaign's folder" : folder;
|
|
191
|
+
return `Tell the player: open ${where} in Antigravity and start a new chat there. This chat runs outside the campaign, and the game's hooks load only when a chat starts in its folder.`;
|
|
192
|
+
}
|
|
193
|
+
/** An answer of a call with a line added after its content; an error answer keeps its error and gets the line as its message's end. */
|
|
194
|
+
function withAdvice(message, advice) {
|
|
195
|
+
const result = message.result;
|
|
196
|
+
if (result !== undefined && Array.isArray(result.content)) {
|
|
197
|
+
return { ...message, result: { ...result, content: [...result.content, { type: "text", text: advice }] } };
|
|
198
|
+
}
|
|
199
|
+
const error = message.error;
|
|
200
|
+
return error === undefined ? message : { ...message, error: { ...error, message: `${String(error.message ?? "")} ${advice}`.trim() } };
|
|
201
|
+
}
|
|
202
|
+
function parsed(line) {
|
|
203
|
+
try {
|
|
204
|
+
const message = JSON.parse(line);
|
|
205
|
+
return message !== null && typeof message === "object" && !Array.isArray(message) ? message : undefined;
|
|
206
|
+
}
|
|
207
|
+
catch {
|
|
208
|
+
return undefined;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function conversationOf(message) {
|
|
212
|
+
const meta = message.params?._meta;
|
|
213
|
+
const conversation = meta?.[conversationKey];
|
|
214
|
+
return typeof conversation === "string" && conversation !== "" ? conversation : undefined;
|
|
215
|
+
}
|
package/dist/themes/css.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { Theme } from "./theme.ts";
|
|
2
|
-
/** The CSS of a theme: its @font-face rules,
|
|
2
|
+
/** The CSS of a theme: its @font-face rules, the layer theme with the values of both modes, the colours of the guarantees; its files at this address, such as /theme. */
|
|
3
3
|
export declare function themeCss(theme: Theme, address: string): string;
|