@minecraftbot-com/plugin 0.1.0 → 0.2.0
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/AGENTS.md +188 -177
- package/README.md +47 -46
- package/dist/api.d.ts +4 -1
- package/dist/cli/api.d.ts +23 -0
- package/dist/cli/api.js +8 -0
- package/dist/cli/build.d.ts +1 -0
- package/dist/cli/build.js +9 -2
- package/dist/cli/check.d.ts +7 -0
- package/dist/cli/check.js +2 -1
- package/dist/cli/deploy.d.ts +13 -0
- package/dist/cli/deploy.js +51 -0
- package/dist/cli/main.js +7 -0
- package/dist/hosted/fetch.d.ts +53 -0
- package/dist/hosted/fetch.js +216 -0
- package/dist/hosted/index.d.ts +3 -0
- package/dist/hosted/index.js +81 -0
- package/dist/runtime/runtime.d.ts +1 -1
- package/dist/runtime/runtime.js +1 -1
- package/package.json +6 -1
- package/src/api.ts +4 -1
- package/src/cli/api.ts +105 -79
- package/src/cli/build.ts +9 -2
- package/src/cli/check.ts +104 -103
- package/src/cli/deploy.ts +60 -0
- package/src/cli/main.ts +7 -0
- package/src/hosted/fetch.ts +224 -0
- package/src/hosted/index.ts +121 -0
- package/src/runtime/runtime.ts +281 -281
- package/templates/basic/AGENTS.md +32 -31
- package/templates/basic/README.md +14 -13
- package/templates/basic/package.json +1 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// @minecraftbot-com/plugin/hosted: what runs a plugin inside MineBot's runner (QuickJS in WebAssembly). `minebot
|
|
2
|
+
// build` and `minebot deploy` wrap the plugin with it; plugin code never imports it.
|
|
3
|
+
//
|
|
4
|
+
// The runner gives the instance its host functions (frames out, closing, its own log, timers, fetch) and drives it through
|
|
5
|
+
// `__minebot`: the bundle's protocol frames are exactly what `minebot dev` sends over its websocket, and the runner
|
|
6
|
+
// carries them to the bot container unchanged.
|
|
7
|
+
import type { DefinedPlugin } from "../plugin.js";
|
|
8
|
+
import type { Transport } from "../runtime/connection.js";
|
|
9
|
+
import { PluginRuntime } from "../runtime/runtime.js";
|
|
10
|
+
import { installFetch } from "./fetch.js";
|
|
11
|
+
|
|
12
|
+
declare function __host_send(text: string): void;
|
|
13
|
+
declare function __host_close(code: number, reason: string): void;
|
|
14
|
+
/** The runner's own log (0 info, 1 warn, 2 error), for what can't reach the plugin console. */
|
|
15
|
+
declare function __host_log(level: number, message: string): void;
|
|
16
|
+
declare function __host_timer_set(id: number, ms: number): void;
|
|
17
|
+
declare function __host_timer_clear(id: number): void;
|
|
18
|
+
|
|
19
|
+
/** What the runner passes to `__minebot.start`. */
|
|
20
|
+
interface HostedConfig {
|
|
21
|
+
token: string;
|
|
22
|
+
/** The deployed version (package.json), sent in the manifest. */
|
|
23
|
+
version: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** What the runner calls; one call is one turn, which runs until the plugin awaits again. */
|
|
27
|
+
interface HostedEntry {
|
|
28
|
+
start(config: string): void;
|
|
29
|
+
receive(frame: string): void;
|
|
30
|
+
timer(id: number): void;
|
|
31
|
+
closed(code: number, reason: string): void;
|
|
32
|
+
/** The outcome of fetch call `id` (JSON). */
|
|
33
|
+
fetchDone(id: number, outcome: string): void;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
type Timer = () => void;
|
|
37
|
+
|
|
38
|
+
function describe(error: unknown): string {
|
|
39
|
+
return error instanceof Error ? (error.stack ?? error.message) : String(error);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** Installs the hosted environment and waits for the runner to start the plugin. */
|
|
43
|
+
export function serveHosted(plugin: DefinedPlugin): void {
|
|
44
|
+
const g = globalThis as Record<string, unknown>;
|
|
45
|
+
|
|
46
|
+
const timers = new Map<number, Timer>();
|
|
47
|
+
let nextTimer = 1;
|
|
48
|
+
g.setTimeout = (fn: Timer, ms?: number): number => {
|
|
49
|
+
const id = nextTimer++;
|
|
50
|
+
timers.set(id, fn);
|
|
51
|
+
__host_timer_set(id, Math.max(0, Math.floor(Number(ms) || 0)));
|
|
52
|
+
return id;
|
|
53
|
+
};
|
|
54
|
+
g.clearTimeout = (id: number): void => {
|
|
55
|
+
if (timers.delete(id)) __host_timer_clear(id);
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const fetchDone = installFetch(g);
|
|
59
|
+
|
|
60
|
+
const transport: Transport = {
|
|
61
|
+
send: text => __host_send(text),
|
|
62
|
+
close: (code, reason) => __host_close(code ?? 1000, reason ?? ""),
|
|
63
|
+
onmessage: null,
|
|
64
|
+
onclose: null,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// console.* goes to the plugin console once connected (before that, to the runner's log), as it would show
|
|
68
|
+
// in the terminal under `minebot dev`.
|
|
69
|
+
let connected = false;
|
|
70
|
+
const write = (level: "debug" | "info" | "warn" | "error", args: unknown[]) => {
|
|
71
|
+
const message = args.map(a => (typeof a === "string" ? a : a instanceof Error ? describe(a) : safeJson(a))).join(" ");
|
|
72
|
+
const trimmed = message.length > 4000 ? `${message.slice(0, 4000)}…` : message;
|
|
73
|
+
if (connected) __host_send(JSON.stringify({ type: "log", bot: null, level, message: trimmed }));
|
|
74
|
+
else __host_log(level === "error" ? 2 : level === "warn" ? 1 : 0, trimmed);
|
|
75
|
+
};
|
|
76
|
+
g.console = {
|
|
77
|
+
log: (...a: unknown[]) => write("info", a),
|
|
78
|
+
info: (...a: unknown[]) => write("info", a),
|
|
79
|
+
debug: (...a: unknown[]) => write("debug", a),
|
|
80
|
+
warn: (...a: unknown[]) => write("warn", a),
|
|
81
|
+
error: (...a: unknown[]) => write("error", a),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const entry: HostedEntry = {
|
|
85
|
+
start(text) {
|
|
86
|
+
const config = JSON.parse(text) as HostedConfig;
|
|
87
|
+
PluginRuntime.connect(plugin, transport, config.token, config.version).then(
|
|
88
|
+
() => (connected = true),
|
|
89
|
+
error => {
|
|
90
|
+
__host_log(2, `Couldn't start: ${describe(error)}`);
|
|
91
|
+
transport.close(1000, "Rejected");
|
|
92
|
+
},
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
receive(frame) {
|
|
96
|
+
transport.onmessage?.(frame);
|
|
97
|
+
},
|
|
98
|
+
timer(id) {
|
|
99
|
+
const fn = timers.get(id);
|
|
100
|
+
if (!fn) return;
|
|
101
|
+
timers.delete(id);
|
|
102
|
+
fn();
|
|
103
|
+
},
|
|
104
|
+
closed(code, reason) {
|
|
105
|
+
connected = false;
|
|
106
|
+
transport.onclose?.(code, reason);
|
|
107
|
+
},
|
|
108
|
+
fetchDone(id, outcome) {
|
|
109
|
+
fetchDone(id, outcome);
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
g.__minebot = entry;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function safeJson(value: unknown): string {
|
|
116
|
+
try {
|
|
117
|
+
return JSON.stringify(value) ?? String(value);
|
|
118
|
+
} catch {
|
|
119
|
+
return String(value);
|
|
120
|
+
}
|
|
121
|
+
}
|