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
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** Whether `folder` is inside a git repository: it, or a folder above it, holds a .git. Git itself need not be installed. */
|
|
2
|
+
export declare function isRepository(folder: string): boolean;
|
|
3
|
+
/** The folder of the repository `folder` is in, the one that holds the .git: it or one above it; undefined outside any. */
|
|
4
|
+
export declare function repositoryRoot(folder: string): string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Commits every change under `folder`, and only there, with `message`, `left` (paths in it) taken out of the repository but left on the disk: true when there
|
|
7
|
+
* was a change, false when there was none. The commit is made on an index of its own, so that what the player staged elsewhere stays staged and out of it;
|
|
8
|
+
* then the repository's index follows it under `folder`. It throws with git's words.
|
|
9
|
+
*/
|
|
10
|
+
export declare function commitFolder(folder: string, message: string, left?: string[]): Promise<boolean>;
|
|
11
|
+
/** Pushes the branch of the repository of `folder` to where it is pushed. It throws with git's words. */
|
|
12
|
+
export declare function push(folder: string): Promise<void>;
|
package/dist/git/git.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Git in a folder, through the git of this computer: whether the folder is in a repository, a commit of its changes, a push. It never asks anything: a git
|
|
2
|
+
// that would ask for a password or a passphrase fails instead, and says why.
|
|
3
|
+
import { spawn } from "node:child_process";
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { existsSync, rmSync } from "node:fs";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { dirname, join, resolve } from "node:path";
|
|
8
|
+
/** Whether `folder` is inside a git repository: it, or a folder above it, holds a .git. Git itself need not be installed. */
|
|
9
|
+
export function isRepository(folder) {
|
|
10
|
+
return repositoryRoot(folder) !== undefined;
|
|
11
|
+
}
|
|
12
|
+
/** The folder of the repository `folder` is in, the one that holds the .git: it or one above it; undefined outside any. */
|
|
13
|
+
export function repositoryRoot(folder) {
|
|
14
|
+
for (let current = resolve(folder);; current = dirname(current)) {
|
|
15
|
+
// A folder, or a file in a worktree or a submodule.
|
|
16
|
+
if (existsSync(join(current, ".git"))) {
|
|
17
|
+
return current;
|
|
18
|
+
}
|
|
19
|
+
if (dirname(current) === current) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Commits every change under `folder`, and only there, with `message`, `left` (paths in it) taken out of the repository but left on the disk: true when there
|
|
26
|
+
* was a change, false when there was none. The commit is made on an index of its own, so that what the player staged elsewhere stays staged and out of it;
|
|
27
|
+
* then the repository's index follows it under `folder`. It throws with git's words.
|
|
28
|
+
*/
|
|
29
|
+
export async function commitFolder(folder, message, left = []) {
|
|
30
|
+
const own = { GIT_INDEX_FILE: join(tmpdir(), `rpg-git-index-${randomUUID()}`) };
|
|
31
|
+
try {
|
|
32
|
+
const before = await hasCommits(folder);
|
|
33
|
+
if (before) {
|
|
34
|
+
await git(folder, ["read-tree", "HEAD"], own);
|
|
35
|
+
}
|
|
36
|
+
await stageFolder(folder, left, own);
|
|
37
|
+
// The changes to HEAD, or in a repository with no commit yet whatever is staged.
|
|
38
|
+
const changes = await git(folder, before ? ["diff", "--cached", "--name-only"] : ["ls-files"], own);
|
|
39
|
+
if (changes.trim() === "") {
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
await git(folder, ["commit", "--quiet", "--message", message], own);
|
|
43
|
+
}
|
|
44
|
+
finally {
|
|
45
|
+
rmSync(own.GIT_INDEX_FILE, { force: true });
|
|
46
|
+
}
|
|
47
|
+
await stageFolder(folder, left);
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
/** Pushes the branch of the repository of `folder` to where it is pushed. It throws with git's words. */
|
|
51
|
+
export async function push(folder) {
|
|
52
|
+
// A branch with no upstream yet gets the one of its name, as the player's first git push -u would.
|
|
53
|
+
await git(folder, ["-c", "push.autoSetupRemote=true", "push", "--quiet"], await sshVariables(folder));
|
|
54
|
+
}
|
|
55
|
+
/** The ssh of the player, as GIT_SSH_COMMAND, GIT_SSH or core.sshCommand say, else ssh that never asks for a passphrase. */
|
|
56
|
+
async function sshVariables(folder) {
|
|
57
|
+
if (process.env.GIT_SSH_COMMAND !== undefined || process.env.GIT_SSH !== undefined) {
|
|
58
|
+
return {};
|
|
59
|
+
}
|
|
60
|
+
try {
|
|
61
|
+
const own = (await git(folder, ["config", "--get", "core.sshCommand"])).trim();
|
|
62
|
+
if (own !== "") {
|
|
63
|
+
// Their command kept; BatchMode only where it is OpenSSH's.
|
|
64
|
+
return { GIT_SSH_COMMAND: /(^|[\\/])ssh(\.exe)?"?(\s|$)/i.test(own) ? `${own} -o BatchMode=yes` : own };
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Unset: git says so with a failure.
|
|
69
|
+
}
|
|
70
|
+
return { GIT_SSH_COMMAND: "ssh -o BatchMode=yes" };
|
|
71
|
+
}
|
|
72
|
+
/** Stages every change under `folder`, `left` taken out, on the index `variables` name, or the repository's. */
|
|
73
|
+
async function stageFolder(folder, left, variables = {}) {
|
|
74
|
+
await git(folder, ["add", "--all", "--", "."], variables);
|
|
75
|
+
if (left.length > 0) {
|
|
76
|
+
await git(folder, ["rm", "-r", "--cached", "--quiet", "--ignore-unmatch", "--", ...left], variables);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async function hasCommits(folder) {
|
|
80
|
+
try {
|
|
81
|
+
await git(folder, ["rev-parse", "--verify", "--quiet", "HEAD"]);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
// How long git may take before it is stopped: a push that waits on a network or on a question never answers.
|
|
89
|
+
const longest = 120_000;
|
|
90
|
+
// What a git that another git holds says: tried once more a moment later.
|
|
91
|
+
const heldByAnother = /index\.lock|cannot lock ref/i;
|
|
92
|
+
/** What git printed, or an error with what it said and how to fix it when it failed, could not start or took too long; once again when another git held it. */
|
|
93
|
+
async function git(folder, args, variables = {}) {
|
|
94
|
+
try {
|
|
95
|
+
return await gitOnce(folder, args, variables);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (!(error instanceof Error) || !heldByAnother.test(error.message)) {
|
|
99
|
+
throw error;
|
|
100
|
+
}
|
|
101
|
+
await new Promise((done) => setTimeout(done, 500));
|
|
102
|
+
return gitOnce(folder, args, variables);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function gitOnce(folder, args, variables) {
|
|
106
|
+
return new Promise((done, fail) => {
|
|
107
|
+
// core.askPass emptied too: an askpass is a program that asks.
|
|
108
|
+
const child = spawn("git", ["-c", "core.askPass=", ...args], { cwd: folder, env: { ...quietEnvironment(), ...variables }, windowsHide: true, stdio: ["ignore", "pipe", "pipe"] });
|
|
109
|
+
let out = "";
|
|
110
|
+
let said = "";
|
|
111
|
+
let settled = false;
|
|
112
|
+
const settle = (work) => {
|
|
113
|
+
if (!settled) {
|
|
114
|
+
settled = true;
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
work();
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
// A git stopped answers at once: its hooks, ssh or the remote's helper may keep its pipes open long after.
|
|
120
|
+
const timer = setTimeout(() => {
|
|
121
|
+
stopTree(child.pid);
|
|
122
|
+
child.stdout.destroy();
|
|
123
|
+
child.stderr.destroy();
|
|
124
|
+
settle(() => fail(new Error(`git ${args[0]} took longer than ${longest / 60_000} minutes and was stopped: look at the network, or at a hook of the repository that waits.`)));
|
|
125
|
+
}, longest);
|
|
126
|
+
child.stdout.on("data", (chunk) => (out += chunk.toString("utf8")));
|
|
127
|
+
child.stderr.on("data", (chunk) => (said += chunk.toString("utf8")));
|
|
128
|
+
child.on("error", (error) => settle(() => fail(new Error(`git could not start (${error.message}): install git, or turn git off in the settings.`))));
|
|
129
|
+
child.on("close", (code) => settle(() => (code === 0 ? done(out) : fail(new Error(gitFailure(said || out, args[0]))))));
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/** What git said when it failed, its errors and hints, with our own fix for the failures a player meets most. */
|
|
133
|
+
function gitFailure(said, command) {
|
|
134
|
+
if (/please tell me who you are|author identity unknown/i.test(said)) {
|
|
135
|
+
return 'git does not know who commits: run git config user.name "Your Name" and git config user.email "you@example.com" in the campaign\'s folder.';
|
|
136
|
+
}
|
|
137
|
+
if (/could not read (username|password)|cannot prompt|terminal prompts disabled|permission denied \(publickey|authentication failed/i.test(said)) {
|
|
138
|
+
return "git has no credentials for the remote: run git push once from a terminal in the campaign's folder and sign in; then the engine pushes with what git keeps.";
|
|
139
|
+
}
|
|
140
|
+
if (/has no upstream branch/i.test(said)) {
|
|
141
|
+
return "the branch has no upstream yet: run git push -u origin with the branch's name once in the campaign's folder.";
|
|
142
|
+
}
|
|
143
|
+
if (/no configured push destination|does not appear to be a git repository/i.test(said)) {
|
|
144
|
+
return "the repository has no remote to push to: add one with git remote add origin and its address, or choose commit alone in the settings.";
|
|
145
|
+
}
|
|
146
|
+
const lines = said.split(/\r?\n/).map((line) => line.trim()).filter((line) => line !== "");
|
|
147
|
+
const told = lines.filter((line) => /^(fatal|error|hint):/i.test(line));
|
|
148
|
+
return (told.length > 0 ? told : lines.slice(0, 6)).join(" ") || `git ${command} stopped`;
|
|
149
|
+
}
|
|
150
|
+
/** Stops a process and every process it started: on Windows by its tree, elsewhere by a kill its children see as their pipes close. */
|
|
151
|
+
function stopTree(pid) {
|
|
152
|
+
if (pid === undefined) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (process.platform === "win32") {
|
|
156
|
+
spawn("taskkill", ["/pid", String(pid), "/T", "/F"], { windowsHide: true, stdio: "ignore" }).on("error", () => undefined);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
process.kill(pid, "SIGKILL");
|
|
161
|
+
}
|
|
162
|
+
catch {
|
|
163
|
+
// Already gone.
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
/** The environment of git: the one of this process, with every question it could ask turned into a failure. */
|
|
167
|
+
function quietEnvironment() {
|
|
168
|
+
return {
|
|
169
|
+
...process.env,
|
|
170
|
+
GIT_TERMINAL_PROMPT: "0",
|
|
171
|
+
GCM_INTERACTIVE: "never",
|
|
172
|
+
GIT_ASKPASS: "",
|
|
173
|
+
SSH_ASKPASS: "",
|
|
174
|
+
};
|
|
175
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// The engine's block of a .gitignore: its lines between two marks, which it writes again whole; the lines around it are the player's and stay as they are.
|
|
2
|
+
const begin = "# rpg-engine: begin (written by rpg-engine; your own lines go outside this block)";
|
|
3
|
+
const end = "# rpg-engine: end";
|
|
4
|
+
/** The text of a .gitignore, or none, with the engine's block holding `lines`: in place of the block it had, else after its own lines. */
|
|
5
|
+
export function withIgnoreBlock(text, lines) {
|
|
6
|
+
const newline = text?.includes("\r\n") === true ? "\r\n" : "\n";
|
|
7
|
+
const block = [begin, ...lines, end].join(newline);
|
|
8
|
+
const kept = (text ?? "").split(/\r?\n/);
|
|
9
|
+
const from = kept.indexOf(begin);
|
|
10
|
+
const to = kept.indexOf(end, from);
|
|
11
|
+
if (from !== -1 && to !== -1) {
|
|
12
|
+
return [...kept.slice(0, from), block, ...kept.slice(to + 1)].join(newline);
|
|
13
|
+
}
|
|
14
|
+
const before = (text ?? "").replace(/(\r?\n)*$/, "");
|
|
15
|
+
return before === "" ? `${block}${newline}` : `${before}${newline}${newline}${block}${newline}`;
|
|
16
|
+
}
|
package/dist/helpers/briefs.d.ts
CHANGED
|
@@ -4,6 +4,10 @@ import type { PictureSubject } from "../core/context.ts";
|
|
|
4
4
|
* which the page crops.
|
|
5
5
|
*/
|
|
6
6
|
export declare function chatBrief(subject: PictureSubject): string;
|
|
7
|
+
/** Antigravity's painted brief, with the SVG drawing for when its image tool fails (D-572). */
|
|
8
|
+
export declare function antigravityBrief(subject: PictureSubject): string;
|
|
9
|
+
/** The SVG brief as the chat draws the picture itself (D-572), as OpenCode: handed back with image, svg, never as a file. */
|
|
10
|
+
export declare function drawnBrief(subject: PictureSubject): string;
|
|
7
11
|
/** The painted brief of a place's or a quest's picture, a portrait or a creature's head, as Codex makes it. */
|
|
8
12
|
export declare function paintedBrief(subject: PictureSubject): string;
|
|
9
13
|
/**
|
package/dist/helpers/briefs.js
CHANGED
|
@@ -5,6 +5,14 @@
|
|
|
5
5
|
export function chatBrief(subject) {
|
|
6
6
|
return paintedBrief(subject).replace(imageAsk, chatAsk).replace("landscape, 21:9", "landscape, 16:9");
|
|
7
7
|
}
|
|
8
|
+
/** Antigravity's painted brief, with the SVG drawing for when its image tool fails (D-572). */
|
|
9
|
+
export function antigravityBrief(subject) {
|
|
10
|
+
return `${chatBrief(subject)}\n${svgInstead}`;
|
|
11
|
+
}
|
|
12
|
+
/** The SVG brief as the chat draws the picture itself (D-572), as OpenCode: handed back with image, svg, never as a file. */
|
|
13
|
+
export function drawnBrief(subject) {
|
|
14
|
+
return `${svgBrief(subject).replace(svgOpening, chatDraw)}\n${handIn}`;
|
|
15
|
+
}
|
|
8
16
|
/** The painted brief of a place's or a quest's picture, a portrait or a creature's head, as Codex makes it. */
|
|
9
17
|
export function paintedBrief(subject) {
|
|
10
18
|
if (subject.kind === "portrait") {
|
|
@@ -97,6 +105,11 @@ const creatureFraming = "a close-up of the head, its face or muzzle centred and
|
|
|
97
105
|
const svgOpening = "Draw exactly one picture as SVG code from the brief below. Reply with the SVG alone, from <svg to </svg>: no words, no code fence.";
|
|
98
106
|
const svgStyle = "Style/medium: vector illustration with depth: layered shapes from far to near, gradients for sky, light and water, far layers paler and bluer, "
|
|
99
107
|
+ "a few soft blurred shapes for mist and glow; a sober palette of muted natural colours";
|
|
108
|
+
// The chat draws the SVG itself, and gives it to the engine in its call.
|
|
109
|
+
const chatDraw = "Draw exactly one new picture from the brief below yourself, as SVG code.";
|
|
110
|
+
const handIn = "Then call image again for the same page with svg holding the code, from <svg to </svg>: write no file, and do not show the code to the player.";
|
|
111
|
+
const svgInstead = "If your image tool fails or you have none, draw it as SVG code instead: one <svg> root with a viewBox of the same proportions, only shapes and gradients, "
|
|
112
|
+
+ "no text, script, style or image, under 40 KB; then call image again for the same page with svg holding the code.";
|
|
100
113
|
const svgRules = "SVG rules: one <svg> root with only the viewBox above and xmlns; only the elements svg, g, defs, path, rect, circle, ellipse, polygon, polyline, line, "
|
|
101
114
|
+ "linearGradient, radialGradient, stop, clipPath, mask, filter, feGaussianBlur; no script, style, text, image, use, a, foreignObject or animation, "
|
|
102
115
|
+ "no event attributes, no href, no url() other than url(#id) of this file; under 40 KB";
|
package/dist/helpers/chosen.d.ts
CHANGED
|
@@ -3,13 +3,17 @@ import type { DocumentReader, PictureMaker, TextWriter } from "../core/context.t
|
|
|
3
3
|
export declare const codexClient = "codex-mcp-client";
|
|
4
4
|
/** Antigravity's chat, as its MCP client names itself: it makes its pictures itself. */
|
|
5
5
|
export declare const antigravityClient = "antigravity-client";
|
|
6
|
+
/** OpenCode's chat, as its MCP client names itself: its master draws the pictures, and OpenCode does the work outside the chat there, and nowhere else (D-578 (3)). */
|
|
7
|
+
export declare const openCodeClient = "opencode";
|
|
6
8
|
/**
|
|
7
9
|
* The pictures of this computer, by the campaign's choice, which `choice` reads when a picture is asked: unset or chat, the chat `host` names, by its own
|
|
8
10
|
* means; codex; elevenlabs, or the name of another image tool of the master's, from the brief. Codex, where it cannot or refuses, leaves the picture to the chat
|
|
9
11
|
* in use; once it refused, it is not asked again while the server lives, until the choice changes.
|
|
10
12
|
*/
|
|
11
13
|
export declare function hostPictures(host: () => string | undefined, choice?: () => string | undefined, codex?: PictureMaker, claude?: PictureMaker): PictureMaker;
|
|
12
|
-
/**
|
|
13
|
-
export declare function
|
|
14
|
-
/** The
|
|
15
|
-
export declare function
|
|
14
|
+
/** A chat that draws its pictures itself as SVG code (D-572), as OpenCode: from the SVG brief image gives it, handed back with image, svg; the engine starts nothing. */
|
|
15
|
+
export declare function chatDrawing(name: string): PictureMaker;
|
|
16
|
+
/** The writer of text of this computer for the chat `host` names (D-406): Codex first, then Claude; in OpenCode, OpenCode before them. */
|
|
17
|
+
export declare function hostWriter(host: () => string | undefined, codex?: TextWriter, claude?: TextWriter, openCode?: TextWriter): TextWriter;
|
|
18
|
+
/** The importer of this computer for the chat `host` names (D-489 (1)): Codex in Codex, else Claude first, which Elena's probe measured, then Codex; in OpenCode, OpenCode before them. */
|
|
19
|
+
export declare function hostReader(host: () => string | undefined, codex?: DocumentReader, claude?: DocumentReader, openCode?: DocumentReader): DocumentReader;
|
package/dist/helpers/chosen.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import { chatBrief } from "./briefs.js";
|
|
1
|
+
import { antigravityBrief, chatBrief, drawnBrief } from "./briefs.js";
|
|
2
2
|
import { claudePictures, claudeReader, claudeWriter } from "./claude.js";
|
|
3
3
|
import { codexPictures, codexReader, codexWriter } from "./codex.js";
|
|
4
|
+
import { openCodeReader, openCodeWriter } from "./opencode.js";
|
|
4
5
|
/** The chat whose own pictures are Codex's, as its MCP client names itself. */
|
|
5
6
|
export const codexClient = "codex-mcp-client";
|
|
6
7
|
/** Antigravity's chat, as its MCP client names itself: it makes its pictures itself. */
|
|
7
8
|
export const antigravityClient = "antigravity-client";
|
|
9
|
+
/** OpenCode's chat, as its MCP client names itself: its master draws the pictures, and OpenCode does the work outside the chat there, and nowhere else (D-578 (3)). */
|
|
10
|
+
export const openCodeClient = "opencode";
|
|
8
11
|
/**
|
|
9
12
|
* The pictures of this computer, by the campaign's choice, which `choice` reads when a picture is asked: unset or chat, the chat `host` names, by its own
|
|
10
13
|
* means; codex; elevenlabs, or the name of another image tool of the master's, from the brief. Codex, where it cannot or refuses, leaves the picture to the chat
|
|
@@ -13,8 +16,14 @@ export const antigravityClient = "antigravity-client";
|
|
|
13
16
|
export function hostPictures(host, choice = () => undefined, codex = codexPictures(), claude = claudePictures()) {
|
|
14
17
|
let refused;
|
|
15
18
|
let lastChoice;
|
|
16
|
-
// The chat in use, by its own means.
|
|
17
|
-
const chat = () =>
|
|
19
|
+
// The chat in use, by its own means: Claude's drawing in Claude Code, whose clients' names start with claude, and where no chat names itself.
|
|
20
|
+
const chat = () => {
|
|
21
|
+
const name = host() ?? "";
|
|
22
|
+
if (name === "" || name.toLowerCase().startsWith("claude")) {
|
|
23
|
+
return claude;
|
|
24
|
+
}
|
|
25
|
+
return name === codexClient ? codex : name === antigravityClient ? antigravityPictures : chatDrawing(chatNames[name] ?? name);
|
|
26
|
+
};
|
|
18
27
|
// The maker the campaign chose, and whether it is Codex apart from the chat in use, which leaves the picture to it where it cannot.
|
|
19
28
|
const chosen = () => {
|
|
20
29
|
const said = choice()?.trim();
|
|
@@ -39,9 +48,9 @@ export function hostPictures(host, choice = () => undefined, codex = codexPictur
|
|
|
39
48
|
if (!other.ready().ready) {
|
|
40
49
|
return { refused: instead.why };
|
|
41
50
|
}
|
|
42
|
-
// A chat that paints itself gets no picture to make after its call answered: the next ones are its own.
|
|
51
|
+
// A chat that paints or draws itself gets no picture to make after its call answered: the next ones are its own.
|
|
43
52
|
if (other.brief !== undefined) {
|
|
44
|
-
return { refused: `${instead.why}; the next pictures are yours to make with your own image tool` };
|
|
53
|
+
return { refused: `${instead.why}; the next pictures are yours to ${drawsItself(other) ? "draw as SVG code and give with image, svg" : "make with your own image tool"}` };
|
|
45
54
|
}
|
|
46
55
|
const made = await other.make(subject);
|
|
47
56
|
return "refused" in made ? { refused: `${instead.why}; ${instead.by} made none either: ${made.refused}` } : { ...made, instead };
|
|
@@ -59,7 +68,7 @@ export function hostPictures(host, choice = () => undefined, codex = codexPictur
|
|
|
59
68
|
}
|
|
60
69
|
const said = codexReady(again);
|
|
61
70
|
const instead = said.ready ? undefined : fallbackOf(said);
|
|
62
|
-
return said.ready || instead === undefined ? said : { ready: true, maker: instead.by, instead };
|
|
71
|
+
return said.ready || instead === undefined ? said : { ready: true, maker: instead.by, ...(drawsItself(chat()) ? { draws: true } : {}), instead };
|
|
63
72
|
},
|
|
64
73
|
make: async (subject) => {
|
|
65
74
|
const { maker, codexApart } = chosen();
|
|
@@ -101,12 +110,27 @@ const unnamedTool = {
|
|
|
101
110
|
make: () => Promise.resolve({ refused: "the master makes the pictures with another tool, which the player has not named yet" }),
|
|
102
111
|
brief: chatBrief,
|
|
103
112
|
};
|
|
104
|
-
// Antigravity paints in its own chat, from the brief the image command gives it: the engine starts nothing.
|
|
113
|
+
// Antigravity paints in its own chat, from the brief the image command gives it, or draws the SVG where its tool fails: the engine starts nothing.
|
|
105
114
|
const antigravityPictures = {
|
|
106
115
|
ready: () => ({ ready: true, maker: "Antigravity" }),
|
|
107
116
|
make: () => Promise.resolve({ refused: "Antigravity makes its pictures in its own chat" }),
|
|
108
|
-
brief:
|
|
117
|
+
brief: antigravityBrief,
|
|
109
118
|
};
|
|
119
|
+
// The chats' names for the player, by the names their MCP clients give themselves; any other is shown as it comes.
|
|
120
|
+
const chatNames = { [openCodeClient]: "OpenCode" };
|
|
121
|
+
/** A chat that draws its pictures itself as SVG code (D-572), as OpenCode: from the SVG brief image gives it, handed back with image, svg; the engine starts nothing. */
|
|
122
|
+
export function chatDrawing(name) {
|
|
123
|
+
return {
|
|
124
|
+
ready: () => ({ ready: true, maker: name, draws: true }),
|
|
125
|
+
make: () => Promise.resolve({ refused: `${name} draws its pictures in its own chat` }),
|
|
126
|
+
brief: drawnBrief,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
// Whether a maker is a chat that draws itself; one with no brief never is, and is not asked.
|
|
130
|
+
function drawsItself(maker) {
|
|
131
|
+
const said = maker.brief === undefined ? undefined : maker.ready();
|
|
132
|
+
return said?.ready === true && said.draws === true;
|
|
133
|
+
}
|
|
110
134
|
// An image tool of the master's own, by its name, as ElevenLabs's: the same brief, and the engine starts nothing.
|
|
111
135
|
function masterTool(name) {
|
|
112
136
|
return {
|
|
@@ -115,17 +139,17 @@ function masterTool(name) {
|
|
|
115
139
|
brief: chatBrief,
|
|
116
140
|
};
|
|
117
141
|
}
|
|
118
|
-
/** The writer of text of this computer for the chat `host` names (D-406)
|
|
119
|
-
export function hostWriter(host, codex = codexWriter(), claude = claudeWriter()) {
|
|
120
|
-
const writers = () => (host() === codexClient ? [codex] : [codex, claude]);
|
|
142
|
+
/** The writer of text of this computer for the chat `host` names (D-406): Codex first, then Claude; in OpenCode, OpenCode before them. */
|
|
143
|
+
export function hostWriter(host, codex = codexWriter(), claude = claudeWriter(), openCode = openCodeWriter()) {
|
|
144
|
+
const writers = () => (host() === codexClient ? [codex] : host() === openCodeClient ? [openCode, codex, claude] : [codex, claude]);
|
|
121
145
|
return {
|
|
122
146
|
ready: () => writers().some((writer) => writer.ready()),
|
|
123
147
|
write: (brief) => (writers().find((writer) => writer.ready()) ?? codex).write(brief),
|
|
124
148
|
};
|
|
125
149
|
}
|
|
126
|
-
/** The importer of this computer for the chat `host` names (D-489 (1)): Codex in Codex, else Claude first, which Elena's probe measured, then Codex. */
|
|
127
|
-
export function hostReader(host, codex = codexReader(), claude = claudeReader()) {
|
|
128
|
-
const readers = () => (host() === codexClient ? [codex] : [claude, codex]);
|
|
150
|
+
/** The importer of this computer for the chat `host` names (D-489 (1)): Codex in Codex, else Claude first, which Elena's probe measured, then Codex; in OpenCode, OpenCode before them. */
|
|
151
|
+
export function hostReader(host, codex = codexReader(), claude = claudeReader(), openCode = openCodeReader()) {
|
|
152
|
+
const readers = () => (host() === codexClient ? [codex] : host() === openCodeClient ? [openCode, claude, codex] : [claude, codex]);
|
|
129
153
|
return {
|
|
130
154
|
ready: () => readers().some((reader) => reader.ready()),
|
|
131
155
|
program: () => readers().find((reader) => reader.ready())?.program?.(),
|
package/dist/helpers/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export { chatBrief, paintedBrief, svgBrief } from "./briefs.ts";
|
|
2
|
-
export { antigravityClient, codexClient, hostPictures, hostReader, hostWriter } from "./chosen.ts";
|
|
1
|
+
export { antigravityBrief, chatBrief, drawnBrief, paintedBrief, svgBrief } from "./briefs.ts";
|
|
2
|
+
export { antigravityClient, chatDrawing, codexClient, hostPictures, hostReader, hostWriter, openCodeClient } from "./chosen.ts";
|
|
3
3
|
export { claudeSays } from "./claude.ts";
|
|
4
4
|
export { codexPictures, codexSays } from "./codex.ts";
|
|
5
|
+
export { openCodeReader, openCodeWriter, readerRules } from "./opencode.ts";
|
|
5
6
|
export { computerHelpers, helperChanges, helperCommand, queueInCodex } from "./programs.ts";
|
|
6
7
|
export { stopProcess } from "./stop.ts";
|
|
7
8
|
export { cleanSvg, svgIn } from "./svg.ts";
|
package/dist/helpers/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
// Codex and Claude Code on the player's computer, found, installed and signed in with the player's yes (D-424), and what they do for the game: pictures made
|
|
2
|
-
// from words (D-308, D-390), Codex's painted else Claude's SVG, the importer's reading and the page's words;
|
|
3
|
-
|
|
4
|
-
export {
|
|
2
|
+
// from words (D-308, D-390), Codex's painted else Claude's SVG, the importer's reading and the page's words; OpenCode for the last two in its own chat
|
|
3
|
+
// (D-578 (3)); injected through Context, so that tests never spend.
|
|
4
|
+
export { antigravityBrief, chatBrief, drawnBrief, paintedBrief, svgBrief } from "./briefs.js";
|
|
5
|
+
export { antigravityClient, chatDrawing, codexClient, hostPictures, hostReader, hostWriter, openCodeClient } from "./chosen.js";
|
|
5
6
|
export { claudeSays } from "./claude.js";
|
|
6
7
|
export { codexPictures, codexSays } from "./codex.js";
|
|
8
|
+
export { openCodeReader, openCodeWriter, readerRules } from "./opencode.js";
|
|
7
9
|
export { computerHelpers, helperChanges, helperCommand, queueInCodex } from "./programs.js";
|
|
8
10
|
export { stopProcess } from "./stop.js";
|
|
9
11
|
export { cleanSvg, svgIn } from "./svg.js";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { DocumentReader, TextWriter } from "../core/context.ts";
|
|
2
|
+
/** A permission rule of an OpenCode agent: the last that matches decides. */
|
|
3
|
+
export interface OpenCodeRule {
|
|
4
|
+
action: string;
|
|
5
|
+
resource: string;
|
|
6
|
+
effect: "allow" | "deny";
|
|
7
|
+
}
|
|
8
|
+
/** OpenCode on this computer as a writer of text (D-406): an agent that may use no tool, in an empty folder of its own. `program` is the line that starts it. */
|
|
9
|
+
export declare function openCodeWriter(program?: string): TextWriter;
|
|
10
|
+
/** OpenCode on this computer as the importer (D-489 (1)): in the import's folder, reading only there and writing only in the folders given. */
|
|
11
|
+
export declare function openCodeReader(program?: string): DocumentReader;
|
|
12
|
+
/** The importer's fence (D-489 (1)), as Elena's probe proved it: everything denied, then reading in the folder and editing in the folders given, never outside it. */
|
|
13
|
+
export declare function readerRules(writable: string[]): OpenCodeRule[];
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// OpenCode does the work outside the chat where it is the chat in use (D-578 (3)): the writer of text (D-406) and the importer (D-489 (1)), with the player's own
|
|
2
|
+
// model, the last one they chose in it, never one or a key the engine picks. Each run has an agent of its own, given in the environment and never as a file in
|
|
3
|
+
// the player's folders, with every tool denied but what it needs; the project's own configuration is left out, and the session it made is deleted as it ends.
|
|
4
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
5
|
+
import { randomBytes } from "node:crypto";
|
|
6
|
+
import { mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
7
|
+
import { tmpdir } from "node:os";
|
|
8
|
+
import { delimiter, dirname, join } from "node:path";
|
|
9
|
+
import { knownReady } from "./programs.js";
|
|
10
|
+
import { stopProcess, stopTree } from "./stop.js";
|
|
11
|
+
// The same limits as Claude's: a brief in three minutes, a run of the importer in thirty.
|
|
12
|
+
const longest = 3 * 60 * 1000;
|
|
13
|
+
const longestReading = 30 * 60 * 1000;
|
|
14
|
+
// How long the deletion of a session may take: it took under a second.
|
|
15
|
+
const deleteWait = 30_000;
|
|
16
|
+
/** OpenCode on this computer as a writer of text (D-406): an agent that may use no tool, in an empty folder of its own. `program` is the line that starts it. */
|
|
17
|
+
export function openCodeWriter(program = "opencode") {
|
|
18
|
+
const ready = openCodeReady(program);
|
|
19
|
+
return {
|
|
20
|
+
ready: () => ready().ready,
|
|
21
|
+
write: async (brief) => {
|
|
22
|
+
const model = ready();
|
|
23
|
+
if (!model.ready || model.maker === undefined) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const folder = mkdtempSync(join(tmpdir(), "rpg-writing-"));
|
|
27
|
+
try {
|
|
28
|
+
const ran = await runAgent(program, model.maker, folder, writerAgent, brief, longest);
|
|
29
|
+
return ran.code === 0 && ran.error === undefined ? ran.text : undefined;
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
rmSync(folder, { recursive: true, force: true });
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/** OpenCode on this computer as the importer (D-489 (1)): in the import's folder, reading only there and writing only in the folders given. */
|
|
38
|
+
export function openCodeReader(program = "opencode") {
|
|
39
|
+
const ready = openCodeReady(program);
|
|
40
|
+
return {
|
|
41
|
+
ready: () => ready().ready,
|
|
42
|
+
program: () => "OpenCode",
|
|
43
|
+
read: async (folder, brief, writable, signal) => {
|
|
44
|
+
const model = ready();
|
|
45
|
+
if (!model.ready || model.maker === undefined) {
|
|
46
|
+
return { failed: model.ready ? "OpenCode has no model" : model.why };
|
|
47
|
+
}
|
|
48
|
+
const ran = await runAgent(program, model.maker, folder, readerAgent(writable), brief, longestReading, signal);
|
|
49
|
+
if (ran.code === 0 && ran.error === undefined && ran.text !== undefined) {
|
|
50
|
+
return { said: ran.text };
|
|
51
|
+
}
|
|
52
|
+
const said = ran.error ?? ran.errors.trim().split("\n").at(-1)?.trim() ?? "";
|
|
53
|
+
return { failed: said === "" ? "OpenCode stopped without an answer" : `OpenCode said: ${said}` };
|
|
54
|
+
},
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
/** The importer's fence (D-489 (1)), as Elena's probe proved it: everything denied, then reading in the folder and editing in the folders given, never outside it. */
|
|
58
|
+
export function readerRules(writable) {
|
|
59
|
+
return [
|
|
60
|
+
{ action: "*", resource: "*", effect: "deny" },
|
|
61
|
+
{ action: "read", resource: "*", effect: "allow" },
|
|
62
|
+
{ action: "glob", resource: "*", effect: "allow" },
|
|
63
|
+
{ action: "grep", resource: "*", effect: "allow" },
|
|
64
|
+
...writable.flatMap((name) => [{ action: "edit", resource: `${name}/*`, effect: "allow" }, { action: "edit", resource: `${name}/**`, effect: "allow" }]),
|
|
65
|
+
{ action: "external_directory", resource: "*", effect: "deny" },
|
|
66
|
+
];
|
|
67
|
+
}
|
|
68
|
+
const writerAgent = { name: "rpg-writer", system: "You write what a brief asks, and only that.", rules: [{ action: "*", resource: "*", effect: "deny" }] };
|
|
69
|
+
function readerAgent(writable) {
|
|
70
|
+
return { name: "rpg-reader", system: "You read a player's documents and write a tabletop adventure from them, as files in this folder.", rules: readerRules(writable) };
|
|
71
|
+
}
|
|
72
|
+
// Whether OpenCode is ready, by the line that starts it: asked once, as the other helpers are, and remembered.
|
|
73
|
+
const readies = new Map();
|
|
74
|
+
function openCodeReady(program) {
|
|
75
|
+
const known = readies.get(program) ?? knownReady(() => modelOf(program));
|
|
76
|
+
readies.set(program, known);
|
|
77
|
+
return known;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The player's model in OpenCode, as its maker, without starting one: OpenCode answers, the last model the player chose (`recent[0]` of model.json in its state
|
|
81
|
+
* folder), and that model among those OpenCode can use now. Not ready otherwise.
|
|
82
|
+
*/
|
|
83
|
+
function modelOf(program) {
|
|
84
|
+
const paths = quietRun(`${program} debug paths`);
|
|
85
|
+
const state = paths === undefined ? undefined : /^state\s+(.+?)\s*$/m.exec(paths)?.[1];
|
|
86
|
+
if (state === undefined) {
|
|
87
|
+
return { ready: false, why: "OpenCode does not answer on this computer" };
|
|
88
|
+
}
|
|
89
|
+
const model = lastModel(join(state, "model.json"));
|
|
90
|
+
if (model === undefined) {
|
|
91
|
+
return { ready: false, why: "OpenCode has no model chosen yet: the player picks one in it" };
|
|
92
|
+
}
|
|
93
|
+
const models = quietRun(`${program} models --standalone`);
|
|
94
|
+
if (models === undefined || !models.split(/\r?\n/).some((line) => line.trim() === model.name)) {
|
|
95
|
+
return { ready: false, why: `OpenCode cannot use its model ${model.name} now` };
|
|
96
|
+
}
|
|
97
|
+
return { ready: true, maker: model.variant === undefined ? model.name : `${model.name}#${model.variant}` };
|
|
98
|
+
}
|
|
99
|
+
/** The last model the player chose in OpenCode, with its variant; undefined where there is none, or one whose name the shell could misread. */
|
|
100
|
+
function lastModel(file) {
|
|
101
|
+
try {
|
|
102
|
+
const json = JSON.parse(readFileSync(file, "utf8"));
|
|
103
|
+
const first = json.recent?.[0];
|
|
104
|
+
const name = typeof first?.providerID === "string" && typeof first.modelID === "string" ? `${first.providerID}/${first.modelID}` : undefined;
|
|
105
|
+
const variant = name === undefined ? undefined : json.variant?.[name];
|
|
106
|
+
const safe = (value) => /^[\w.:/@+-]+$/.test(value);
|
|
107
|
+
if (name === undefined || !safe(name)) {
|
|
108
|
+
return undefined;
|
|
109
|
+
}
|
|
110
|
+
return typeof variant === "string" && safe(variant) ? { name, variant } : { name };
|
|
111
|
+
}
|
|
112
|
+
catch {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** What a quick command of OpenCode printed, from a folder of no campaign; undefined where it failed. */
|
|
117
|
+
function quietRun(line) {
|
|
118
|
+
const run = spawnSync(line, { shell: true, encoding: "utf8", timeout: 15_000, windowsHide: true, cwd: tmpdir(), env: openCodeEnvironment() });
|
|
119
|
+
return run.error === undefined && run.status === 0 ? run.stdout : undefined;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* One run of an agent: `opencode run` with a session whose name the engine chose, deleted however the run ends; the answer is the last text part of its
|
|
123
|
+
* events, as a model may write two whole answers in two steps.
|
|
124
|
+
*/
|
|
125
|
+
function runAgent(program, model, folder, agent, brief, limit, signal) {
|
|
126
|
+
const session = `ses_rpg${randomBytes(10).toString("hex")}`;
|
|
127
|
+
const config = { agents: { [agent.name]: { description: agent.system, mode: "primary", system: agent.system, permissions: agent.rules } } };
|
|
128
|
+
const environment = { ...openCodeEnvironment(), OPENCODE_CONFIG_CONTENT: JSON.stringify(config), OPENCODE_DISABLE_PROJECT_CONFIG: "1" };
|
|
129
|
+
const line = `${program} run --standalone --agent ${agent.name} -m "${model}" --format json --archived --session ${session}`;
|
|
130
|
+
return new Promise((done) => {
|
|
131
|
+
// Off Windows it leads a group of its own: the shell alone, stopped, would leave OpenCode running with the output held open.
|
|
132
|
+
const child = spawn(line, { shell: true, windowsHide: true, cwd: folder, env: environment, stdio: ["pipe", "pipe", "pipe"], detached: process.platform !== "win32" });
|
|
133
|
+
// Dropped while it runs: OpenCode stops with its own server.
|
|
134
|
+
const stop = () => (process.platform === "win32" || child.pid === undefined ? stopTree(child) : stopProcess(child.pid));
|
|
135
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
136
|
+
let output = "";
|
|
137
|
+
let errors = "";
|
|
138
|
+
const timer = setTimeout(stop, limit);
|
|
139
|
+
child.stdout.setEncoding("utf8");
|
|
140
|
+
child.stderr.setEncoding("utf8");
|
|
141
|
+
child.stdout.on("data", (chunk) => (output += chunk));
|
|
142
|
+
child.stderr.on("data", (chunk) => (errors += chunk));
|
|
143
|
+
child.stdin.on("error", () => undefined);
|
|
144
|
+
child.on("error", () => undefined);
|
|
145
|
+
child.on("close", (code) => {
|
|
146
|
+
clearTimeout(timer);
|
|
147
|
+
signal?.removeEventListener("abort", stop);
|
|
148
|
+
const events = eventsOf(output);
|
|
149
|
+
const text = events.filter((event) => event.type === "text").map((event) => event.part?.text).filter((one) => typeof one === "string" && one.trim() !== "").at(-1);
|
|
150
|
+
const failed = events.find((event) => event.type === "error");
|
|
151
|
+
const error = failed === undefined ? undefined : errorText(failed);
|
|
152
|
+
void deleteSession(program, session).then(() => done({ code, errors, ...(typeof text === "string" ? { text: text.trim() } : {}), ...(error === undefined ? {} : { error }) }));
|
|
153
|
+
});
|
|
154
|
+
child.stdin.end(brief);
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
/** Deletes a session of the engine's from the player's OpenCode, whether the run made it or not. */
|
|
158
|
+
function deleteSession(program, session) {
|
|
159
|
+
return new Promise((done) => {
|
|
160
|
+
const child = spawn(`${program} session delete ${session} --standalone`, { shell: true, windowsHide: true, cwd: tmpdir(), env: openCodeEnvironment(), stdio: "ignore" });
|
|
161
|
+
const timer = setTimeout(() => stopTree(child), deleteWait);
|
|
162
|
+
child.on("error", () => undefined);
|
|
163
|
+
child.on("close", () => {
|
|
164
|
+
clearTimeout(timer);
|
|
165
|
+
done();
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
function eventsOf(output) {
|
|
170
|
+
return output.split("\n").flatMap((line) => {
|
|
171
|
+
try {
|
|
172
|
+
const event = JSON.parse(line);
|
|
173
|
+
return typeof event === "object" && event !== null ? [event] : [];
|
|
174
|
+
}
|
|
175
|
+
catch {
|
|
176
|
+
return [];
|
|
177
|
+
}
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
function errorText(event) {
|
|
181
|
+
const message = event.error?.data?.message ?? event.error?.name;
|
|
182
|
+
return typeof message === "string" && message !== "" ? message : "it failed";
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* The environment of OpenCode's runs: the chat's own, without what names the campaign, as the chat's session and folder, nor what OpenCode would take from the
|
|
186
|
+
* chat; the player's keys and settings stay, as their model needs them. On Windows the shell does not look for programs in the folder it starts in.
|
|
187
|
+
*/
|
|
188
|
+
function openCodeEnvironment() {
|
|
189
|
+
// The folder the chat runs in, a campaign's; never a root, which every path would name.
|
|
190
|
+
const here = dirname(process.cwd()) === process.cwd() ? "\u0000" : process.cwd().toLowerCase();
|
|
191
|
+
const chats = /^(RPG_|OPENCODE_(SESSION_ID|CLIENT|CONFIG_CONTENT|DISABLE_PROJECT_CONFIG|SERVER_PASSWORD)$|PWD$|OLDPWD$|INIT_CWD$)/i;
|
|
192
|
+
const kept = Object.entries(process.env).flatMap(([variable, value]) => {
|
|
193
|
+
if (value === undefined || chats.test(variable)) {
|
|
194
|
+
return [];
|
|
195
|
+
}
|
|
196
|
+
if (variable.toUpperCase() === "PATH") {
|
|
197
|
+
return [[variable, value.split(delimiter).filter((folder) => !folder.toLowerCase().startsWith(here)).join(delimiter)]];
|
|
198
|
+
}
|
|
199
|
+
return value.toLowerCase().includes(here) ? [] : [[variable, value]];
|
|
200
|
+
});
|
|
201
|
+
return { ...Object.fromEntries(kept), NoDefaultCurrentDirectoryInExePath: "1" };
|
|
202
|
+
}
|