@minecraftbot-com/plugin 0.0.0-stage → 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 -0
- package/README.md +47 -3
- package/dist/api.d.ts +354 -0
- package/dist/api.js +2 -0
- package/dist/cli/api.d.ts +49 -0
- package/dist/cli/api.js +69 -0
- package/dist/cli/build.d.ts +24 -0
- package/dist/cli/build.js +72 -0
- package/dist/cli/check.d.ts +17 -0
- package/dist/cli/check.js +99 -0
- package/dist/cli/config.d.ts +11 -0
- package/dist/cli/config.js +38 -0
- package/dist/cli/create.d.ts +2 -0
- package/dist/cli/create.js +39 -0
- package/dist/cli/deploy.d.ts +13 -0
- package/dist/cli/deploy.js +51 -0
- package/dist/cli/dev-mcp.d.ts +36 -0
- package/dist/cli/dev-mcp.js +101 -0
- package/dist/cli/dev.d.ts +15 -0
- package/dist/cli/dev.js +261 -0
- package/dist/cli/fetch-guard.d.ts +7 -0
- package/dist/cli/fetch-guard.js +66 -0
- package/dist/cli/main.d.ts +2 -0
- package/dist/cli/main.js +119 -0
- package/dist/cli/mcp.d.ts +24 -0
- package/dist/cli/mcp.js +136 -0
- package/dist/cli/output.d.ts +11 -0
- package/dist/cli/output.js +25 -0
- package/dist/cli/test.d.ts +2 -0
- package/dist/cli/test.js +39 -0
- package/dist/errors.d.ts +22 -0
- package/dist/errors.js +35 -0
- package/dist/generated/block-states.d.ts +4087 -0
- package/dist/generated/block-states.js +2 -0
- package/dist/generated/blocks.d.ts +1289 -0
- package/dist/generated/blocks.js +1291 -0
- package/dist/generated/contracts.d.ts +304 -0
- package/dist/generated/contracts.js +327 -0
- package/dist/generated/effects.d.ts +43 -0
- package/dist/generated/effects.js +45 -0
- package/dist/generated/entities.d.ts +164 -0
- package/dist/generated/entities.js +166 -0
- package/dist/generated/items.d.ts +1661 -0
- package/dist/generated/items.js +1663 -0
- package/dist/generated/operations.d.ts +7 -0
- package/dist/generated/operations.js +65 -0
- package/dist/generated/permission-labels.d.ts +3 -0
- package/dist/generated/permission-labels.js +17 -0
- package/dist/generated/version.d.ts +2 -0
- package/dist/generated/version.js +3 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.js +5 -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/index.d.ts +19 -0
- package/dist/index.js +13 -0
- package/dist/permissions.d.ts +11 -0
- package/dist/permissions.js +18 -0
- package/dist/plugin.d.ts +86 -0
- package/dist/plugin.js +26 -0
- package/dist/protocol.d.ts +167 -0
- package/dist/protocol.js +4 -0
- package/dist/runtime/connection.d.ts +52 -0
- package/dist/runtime/connection.js +132 -0
- package/dist/runtime/handles.d.ts +95 -0
- package/dist/runtime/handles.js +289 -0
- package/dist/runtime/memory-transport.d.ts +6 -0
- package/dist/runtime/memory-transport.js +39 -0
- package/dist/runtime/runtime.d.ts +57 -0
- package/dist/runtime/runtime.js +265 -0
- package/dist/runtime/scope.d.ts +25 -0
- package/dist/runtime/scope.js +94 -0
- package/dist/runtime/storage-runtime.d.ts +10 -0
- package/dist/runtime/storage-runtime.js +131 -0
- package/dist/settings.d.ts +111 -0
- package/dist/settings.js +78 -0
- package/dist/storage.d.ts +78 -0
- package/dist/storage.js +24 -0
- package/dist/testing/fake-container.d.ts +175 -0
- package/dist/testing/fake-container.js +483 -0
- package/dist/testing/index.d.ts +37 -0
- package/dist/testing/index.js +54 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +54 -0
- package/llms.txt +11 -0
- package/package.json +60 -4
- package/src/api.ts +337 -0
- package/src/cli/api.ts +105 -0
- package/src/cli/build.ts +83 -0
- package/src/cli/check.ts +104 -0
- package/src/cli/config.ts +48 -0
- package/src/cli/create.ts +38 -0
- package/src/cli/deploy.ts +60 -0
- package/src/cli/dev-mcp.ts +131 -0
- package/src/cli/dev.ts +272 -0
- package/src/cli/fetch-guard.ts +61 -0
- package/src/cli/main.ts +120 -0
- package/src/cli/mcp.ts +168 -0
- package/src/cli/output.ts +30 -0
- package/src/cli/test.ts +38 -0
- package/src/errors.ts +34 -0
- package/src/generated/block-states.ts +892 -0
- package/src/generated/blocks.ts +1291 -0
- package/src/generated/contracts.ts +329 -0
- package/src/generated/effects.ts +45 -0
- package/src/generated/entities.ts +166 -0
- package/src/generated/items.ts +1663 -0
- package/src/generated/operations.ts +69 -0
- package/src/generated/permission-labels.ts +19 -0
- package/src/generated/version.ts +3 -0
- package/src/host/index.ts +9 -0
- package/src/hosted/fetch.ts +224 -0
- package/src/hosted/index.ts +121 -0
- package/src/index.ts +34 -0
- package/src/permissions.ts +23 -0
- package/src/plugin.ts +105 -0
- package/src/protocol.ts +107 -0
- package/src/runtime/connection.ts +167 -0
- package/src/runtime/handles.ts +337 -0
- package/src/runtime/memory-transport.ts +41 -0
- package/src/runtime/runtime.ts +281 -0
- package/src/runtime/scope.ts +101 -0
- package/src/runtime/storage-runtime.ts +138 -0
- package/src/settings.ts +118 -0
- package/src/storage.ts +82 -0
- package/src/testing/fake-container.ts +571 -0
- package/src/testing/index.ts +70 -0
- package/src/types.ts +88 -0
- package/templates/basic/AGENTS.md +32 -0
- package/templates/basic/README.md +14 -0
- package/templates/basic/gitignore +3 -0
- package/templates/basic/package.json +19 -0
- package/templates/basic/src/index.ts +20 -0
- package/templates/basic/test/plugin.test.ts +14 -0
- package/templates/basic/tsconfig.json +14 -0
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
2
|
+
/** The manifest the CLI builds from `definePlugin` and the plugin sends in `hello`. */
|
|
3
|
+
export interface Manifest {
|
|
4
|
+
name: string;
|
|
5
|
+
version: string;
|
|
6
|
+
permissions: string[];
|
|
7
|
+
exclusive: string[];
|
|
8
|
+
settings: Record<string, SettingSchema>;
|
|
9
|
+
storage: Record<string, StorageSchema>;
|
|
10
|
+
/** True when the plugin defines `runProfile`. */
|
|
11
|
+
profile: boolean;
|
|
12
|
+
}
|
|
13
|
+
export interface SettingSchema {
|
|
14
|
+
type: "text" | "number" | "boolean" | "block" | "item" | "entity" | "url" | "secret" | "select" | "radius" | "position" | "list";
|
|
15
|
+
label: string;
|
|
16
|
+
description?: string;
|
|
17
|
+
optional?: boolean;
|
|
18
|
+
default?: unknown;
|
|
19
|
+
min?: number;
|
|
20
|
+
max?: number;
|
|
21
|
+
options?: unknown[];
|
|
22
|
+
/** `url`: the hosts a user may enter. Empty or missing means any public HTTPS host. */
|
|
23
|
+
hosts?: string[];
|
|
24
|
+
secret?: boolean;
|
|
25
|
+
/** `list`: the schema of one entry. */
|
|
26
|
+
of?: SettingSchema;
|
|
27
|
+
}
|
|
28
|
+
export interface StorageSchema {
|
|
29
|
+
kind: "value" | "map";
|
|
30
|
+
scope: string;
|
|
31
|
+
default?: unknown;
|
|
32
|
+
}
|
|
33
|
+
export interface Limits {
|
|
34
|
+
callsPerSecond: number;
|
|
35
|
+
concurrentCalls: number;
|
|
36
|
+
frameBytes: number;
|
|
37
|
+
eventQueue: number;
|
|
38
|
+
storageBytes: number;
|
|
39
|
+
storageValueBytes: number;
|
|
40
|
+
}
|
|
41
|
+
export interface BotInfo {
|
|
42
|
+
id: string;
|
|
43
|
+
name: string;
|
|
44
|
+
uuid: string;
|
|
45
|
+
connected: boolean;
|
|
46
|
+
enabled: boolean;
|
|
47
|
+
}
|
|
48
|
+
export interface ProfileInfo {
|
|
49
|
+
id: string;
|
|
50
|
+
name: string;
|
|
51
|
+
}
|
|
52
|
+
/** Only honoured for dev tokens: which bots to run on and the settings to use (the dashboard doesn't know a dev session). */
|
|
53
|
+
export interface DevOptions {
|
|
54
|
+
/** Bot ids, names or uuids; null runs on every bot of the profile. */
|
|
55
|
+
bots: string[] | null;
|
|
56
|
+
settings: Record<string, unknown>;
|
|
57
|
+
}
|
|
58
|
+
export interface HelloFrame {
|
|
59
|
+
type: "hello";
|
|
60
|
+
protocol: number;
|
|
61
|
+
sdk: string;
|
|
62
|
+
token: string;
|
|
63
|
+
manifest: Manifest;
|
|
64
|
+
dev?: DevOptions;
|
|
65
|
+
}
|
|
66
|
+
export interface CallFrame {
|
|
67
|
+
type: "call";
|
|
68
|
+
id: number;
|
|
69
|
+
bot: string | null;
|
|
70
|
+
op: string;
|
|
71
|
+
args: Record<string, unknown>;
|
|
72
|
+
}
|
|
73
|
+
export interface CancelFrame {
|
|
74
|
+
type: "cancel";
|
|
75
|
+
id: number;
|
|
76
|
+
}
|
|
77
|
+
export interface SubscribeFrame {
|
|
78
|
+
type: "subscribe";
|
|
79
|
+
bot: string | null;
|
|
80
|
+
event: string;
|
|
81
|
+
}
|
|
82
|
+
export interface UnsubscribeFrame {
|
|
83
|
+
type: "unsubscribe";
|
|
84
|
+
bot: string | null;
|
|
85
|
+
event: string;
|
|
86
|
+
}
|
|
87
|
+
export interface LogFrame {
|
|
88
|
+
type: "log";
|
|
89
|
+
bot: string | null;
|
|
90
|
+
level: "debug" | "info" | "warn" | "error";
|
|
91
|
+
message: string;
|
|
92
|
+
}
|
|
93
|
+
export interface WarningFrame {
|
|
94
|
+
type: "warning";
|
|
95
|
+
bot: string | null;
|
|
96
|
+
key: string;
|
|
97
|
+
message: string | null;
|
|
98
|
+
}
|
|
99
|
+
export interface StatFrame {
|
|
100
|
+
type: "stat";
|
|
101
|
+
bot: string | null;
|
|
102
|
+
key: string;
|
|
103
|
+
value: number;
|
|
104
|
+
}
|
|
105
|
+
/** The plugin finished a run(bot) on its own (returned or threw), so the container can show it stopped. */
|
|
106
|
+
export interface DoneFrame {
|
|
107
|
+
type: "done";
|
|
108
|
+
bot: string | null;
|
|
109
|
+
error: string | null;
|
|
110
|
+
}
|
|
111
|
+
export type PluginFrame = HelloFrame | CallFrame | CancelFrame | SubscribeFrame | UnsubscribeFrame | LogFrame | WarningFrame | StatFrame | DoneFrame;
|
|
112
|
+
export interface WelcomeFrame {
|
|
113
|
+
type: "welcome";
|
|
114
|
+
installation: string;
|
|
115
|
+
mode: "Dev" | "Hosted";
|
|
116
|
+
profile: ProfileInfo;
|
|
117
|
+
bots: BotInfo[];
|
|
118
|
+
granted: string[];
|
|
119
|
+
settings: Record<string, unknown>;
|
|
120
|
+
limits: Limits;
|
|
121
|
+
}
|
|
122
|
+
export interface RejectedFrame {
|
|
123
|
+
type: "rejected";
|
|
124
|
+
reason: string;
|
|
125
|
+
message: string;
|
|
126
|
+
}
|
|
127
|
+
export interface ResultFrame {
|
|
128
|
+
type: "result";
|
|
129
|
+
id: number;
|
|
130
|
+
ok: boolean;
|
|
131
|
+
value?: unknown;
|
|
132
|
+
failure?: string;
|
|
133
|
+
message?: string;
|
|
134
|
+
}
|
|
135
|
+
export interface ErrorFrame {
|
|
136
|
+
type: "error";
|
|
137
|
+
id: number | null;
|
|
138
|
+
code: string;
|
|
139
|
+
message: string;
|
|
140
|
+
}
|
|
141
|
+
/** `version` is the server's Minecraft version for a bot. */
|
|
142
|
+
export interface StartFrame {
|
|
143
|
+
type: "start";
|
|
144
|
+
bot: string | null;
|
|
145
|
+
settings: Record<string, unknown>;
|
|
146
|
+
version?: string;
|
|
147
|
+
}
|
|
148
|
+
export interface StopFrame {
|
|
149
|
+
type: "stop";
|
|
150
|
+
bot: string | null;
|
|
151
|
+
reason: string;
|
|
152
|
+
}
|
|
153
|
+
export interface EventFrame {
|
|
154
|
+
type: "event";
|
|
155
|
+
bot: string | null;
|
|
156
|
+
event: string;
|
|
157
|
+
data: unknown;
|
|
158
|
+
}
|
|
159
|
+
export interface DroppedFrame {
|
|
160
|
+
type: "dropped";
|
|
161
|
+
count: number;
|
|
162
|
+
}
|
|
163
|
+
export interface BotsFrame {
|
|
164
|
+
type: "bots";
|
|
165
|
+
bots: BotInfo[];
|
|
166
|
+
}
|
|
167
|
+
export type ContainerFrame = WelcomeFrame | RejectedFrame | ResultFrame | ErrorFrame | StartFrame | StopFrame | EventFrame | DroppedFrame | BotsFrame;
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
// The wire protocol between a plugin and the bot container (protocol v1). JSON text frames, one websocket
|
|
2
|
+
// per running plugin instance. The bot container is the authority: it checks every frame, so anything here
|
|
3
|
+
// is a convenience for the SDK, not a guarantee.
|
|
4
|
+
export const PROTOCOL_VERSION = 1;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { type ContainerFrame, type DevOptions, type Manifest, type PluginFrame, type WelcomeFrame } from "../protocol.js";
|
|
2
|
+
import type { Scope } from "./scope.js";
|
|
3
|
+
/** What carries frames: a websocket locally, the runner's host functions when hosted, memory in tests. */
|
|
4
|
+
export interface Transport {
|
|
5
|
+
send(text: string): void;
|
|
6
|
+
close(code?: number, reason?: string): void;
|
|
7
|
+
/** Set by the connection before the first frame arrives. */
|
|
8
|
+
onmessage: ((text: string) => void) | null;
|
|
9
|
+
onclose: ((code: number, reason: string) => void) | null;
|
|
10
|
+
}
|
|
11
|
+
/** The outcome of a call, before the bot API turns it into a value or a Result. */
|
|
12
|
+
export type CallOutcome = {
|
|
13
|
+
ok: true;
|
|
14
|
+
value: unknown;
|
|
15
|
+
} | {
|
|
16
|
+
ok: false;
|
|
17
|
+
failure: string;
|
|
18
|
+
message: string;
|
|
19
|
+
};
|
|
20
|
+
export interface ConnectionHandlers {
|
|
21
|
+
start(bot: string | null, settings: Record<string, unknown>, version: string): void;
|
|
22
|
+
stop(bot: string | null, reason: string): void;
|
|
23
|
+
event(bot: string | null, event: string, data: unknown): void;
|
|
24
|
+
bots(frame: Extract<ContainerFrame, {
|
|
25
|
+
type: "bots";
|
|
26
|
+
}>): void;
|
|
27
|
+
dropped(count: number): void;
|
|
28
|
+
/** The connection closed; every scope is stopped by the caller. */
|
|
29
|
+
closed(code: number, reason: string): void;
|
|
30
|
+
}
|
|
31
|
+
/** One plugin instance's socket: the hello handshake, call ids, and routing of what comes back. */
|
|
32
|
+
export declare class Connection {
|
|
33
|
+
private readonly transport;
|
|
34
|
+
private nextId;
|
|
35
|
+
private readonly pending;
|
|
36
|
+
private handlers;
|
|
37
|
+
private welcomeWaiter;
|
|
38
|
+
private closed;
|
|
39
|
+
constructor(transport: Transport);
|
|
40
|
+
/** Sends `hello` and waits for `welcome`. Rejects with the container's reason if it refuses. */
|
|
41
|
+
open(token: string, manifest: Manifest, sdk: string, dev?: DevOptions): Promise<WelcomeFrame>;
|
|
42
|
+
attach(handlers: ConnectionHandlers): void;
|
|
43
|
+
send(frame: PluginFrame): void;
|
|
44
|
+
/**
|
|
45
|
+
* Sends a call and waits for its result within `scope`. When the scope stops first, the call is cancelled
|
|
46
|
+
* on the container and the promise rejects with StoppedError.
|
|
47
|
+
*/
|
|
48
|
+
call(scope: Scope, bot: string | null, op: string, args: Record<string, unknown>): Promise<CallOutcome>;
|
|
49
|
+
close(): void;
|
|
50
|
+
private receive;
|
|
51
|
+
private onClose;
|
|
52
|
+
}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { PermissionDeniedError, PluginError, StoppedError } from "../errors.js";
|
|
2
|
+
import { PROTOCOL_VERSION, } from "../protocol.js";
|
|
3
|
+
/** One plugin instance's socket: the hello handshake, call ids, and routing of what comes back. */
|
|
4
|
+
export class Connection {
|
|
5
|
+
transport;
|
|
6
|
+
nextId = 1;
|
|
7
|
+
pending = new Map();
|
|
8
|
+
handlers = null;
|
|
9
|
+
welcomeWaiter = null;
|
|
10
|
+
closed = false;
|
|
11
|
+
constructor(transport) {
|
|
12
|
+
this.transport = transport;
|
|
13
|
+
transport.onmessage = text => this.receive(text);
|
|
14
|
+
transport.onclose = (code, reason) => this.onClose(code, reason);
|
|
15
|
+
}
|
|
16
|
+
/** Sends `hello` and waits for `welcome`. Rejects with the container's reason if it refuses. */
|
|
17
|
+
open(token, manifest, sdk, dev) {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
this.welcomeWaiter = { resolve, reject };
|
|
20
|
+
this.send({ type: "hello", protocol: PROTOCOL_VERSION, sdk, token, manifest, ...(dev ? { dev } : {}) });
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
attach(handlers) {
|
|
24
|
+
this.handlers = handlers;
|
|
25
|
+
}
|
|
26
|
+
send(frame) {
|
|
27
|
+
if (this.closed)
|
|
28
|
+
return;
|
|
29
|
+
this.transport.send(JSON.stringify(frame));
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Sends a call and waits for its result within `scope`. When the scope stops first, the call is cancelled
|
|
33
|
+
* on the container and the promise rejects with StoppedError.
|
|
34
|
+
*/
|
|
35
|
+
call(scope, bot, op, args) {
|
|
36
|
+
scope.check();
|
|
37
|
+
if (this.closed)
|
|
38
|
+
return Promise.reject(new StoppedError("Disconnected"));
|
|
39
|
+
const id = this.nextId++;
|
|
40
|
+
return new Promise((resolve, reject) => {
|
|
41
|
+
const cancel = () => {
|
|
42
|
+
if (this.pending.delete(id)) {
|
|
43
|
+
this.send({ type: "cancel", id });
|
|
44
|
+
reject(new StoppedError(scope.reason ?? "Stopped"));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
scope.onStop(cancel);
|
|
48
|
+
this.pending.set(id, {
|
|
49
|
+
resolve: outcome => {
|
|
50
|
+
scope.forget(cancel);
|
|
51
|
+
resolve(outcome);
|
|
52
|
+
},
|
|
53
|
+
reject: error => {
|
|
54
|
+
scope.forget(cancel);
|
|
55
|
+
reject(error);
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
this.send({ type: "call", id, bot, op, args });
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
close() {
|
|
62
|
+
this.transport.close(1000, "Plugin stopped");
|
|
63
|
+
}
|
|
64
|
+
receive(text) {
|
|
65
|
+
let frame;
|
|
66
|
+
try {
|
|
67
|
+
frame = JSON.parse(text);
|
|
68
|
+
}
|
|
69
|
+
catch {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
switch (frame.type) {
|
|
73
|
+
case "welcome":
|
|
74
|
+
this.welcomeWaiter?.resolve(frame);
|
|
75
|
+
this.welcomeWaiter = null;
|
|
76
|
+
break;
|
|
77
|
+
case "rejected":
|
|
78
|
+
this.welcomeWaiter?.reject(new PluginError(frame.reason, frame.message));
|
|
79
|
+
this.welcomeWaiter = null;
|
|
80
|
+
break;
|
|
81
|
+
case "result": {
|
|
82
|
+
const p = this.pending.get(frame.id);
|
|
83
|
+
if (!p)
|
|
84
|
+
break;
|
|
85
|
+
this.pending.delete(frame.id);
|
|
86
|
+
p.resolve(frame.ok ? { ok: true, value: frame.value } : { ok: false, failure: frame.failure ?? "Failed", message: frame.message ?? "" });
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
case "error": {
|
|
90
|
+
const error = frame.code === "PermissionDenied" ? new PermissionDeniedError(frame.message) : new PluginError(frame.code, frame.message);
|
|
91
|
+
if (frame.id === null) {
|
|
92
|
+
// A connection-level error (for example a frame the container refused); log through the handler.
|
|
93
|
+
this.handlers?.event(null, "__error", { code: frame.code, message: frame.message });
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
const p = this.pending.get(frame.id);
|
|
97
|
+
if (!p)
|
|
98
|
+
break;
|
|
99
|
+
this.pending.delete(frame.id);
|
|
100
|
+
p.reject(error);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case "start":
|
|
104
|
+
this.handlers?.start(frame.bot, frame.settings ?? {}, frame.version ?? "");
|
|
105
|
+
break;
|
|
106
|
+
case "stop":
|
|
107
|
+
this.handlers?.stop(frame.bot, frame.reason);
|
|
108
|
+
break;
|
|
109
|
+
case "event":
|
|
110
|
+
this.handlers?.event(frame.bot, frame.event, frame.data);
|
|
111
|
+
break;
|
|
112
|
+
case "bots":
|
|
113
|
+
this.handlers?.bots(frame);
|
|
114
|
+
break;
|
|
115
|
+
case "dropped":
|
|
116
|
+
this.handlers?.dropped(frame.count);
|
|
117
|
+
break;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
onClose(code, reason) {
|
|
121
|
+
if (this.closed)
|
|
122
|
+
return;
|
|
123
|
+
this.closed = true;
|
|
124
|
+
const error = new StoppedError("Disconnected");
|
|
125
|
+
for (const p of this.pending.values())
|
|
126
|
+
p.reject(error);
|
|
127
|
+
this.pending.clear();
|
|
128
|
+
this.welcomeWaiter?.reject(new PluginError("Disconnected", reason || `Connection closed (${code})`));
|
|
129
|
+
this.welcomeWaiter = null;
|
|
130
|
+
this.handlers?.closed(code, reason);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { Bot, BotEventData, BotPlugins, BotStatus, Containers, Entities, EntityInfo, Inventory, Listener, Profile, ProfileBot, ProfileEventData, Recipe, World } from "../api.js";
|
|
2
|
+
import type { BotEvent, CraftFailure, ProfileEvent } from "../generated/contracts.js";
|
|
3
|
+
import type { Block } from "../generated/blocks.js";
|
|
4
|
+
import type { Item } from "../generated/items.js";
|
|
5
|
+
import type { Entity } from "../generated/entities.js";
|
|
6
|
+
import type { BotInfo } from "../protocol.js";
|
|
7
|
+
import { type Duration, type Position, type Result, type Vec3 } from "../types.js";
|
|
8
|
+
import type { CallOutcome } from "./connection.js";
|
|
9
|
+
import type { Scope } from "./scope.js";
|
|
10
|
+
/** What the handles need from the runtime: calls, subscriptions and logging, all within a scope. */
|
|
11
|
+
export interface HandleHost {
|
|
12
|
+
call(scope: Scope, bot: string | null, op: string, args?: Record<string, unknown>): Promise<CallOutcome>;
|
|
13
|
+
subscribe(scope: Scope, bot: string | null, event: string, listener: (data: unknown) => unknown): () => void;
|
|
14
|
+
botInfo(id: string): BotInfo | undefined;
|
|
15
|
+
profileBot(id: string): ProfileBot;
|
|
16
|
+
logError(bot: string | null, error: unknown): void;
|
|
17
|
+
}
|
|
18
|
+
/** `bot.loop` and `profile.loop`: repeat until the scope stops, with a floor between runs and backoff on errors. */
|
|
19
|
+
export declare function loop(scope: Scope, host: HandleHost, bot: string | null, body: () => unknown, every?: Duration): Promise<never>;
|
|
20
|
+
/** A bot of the profile as seen from a scope: live name and state from the runtime's bot list. */
|
|
21
|
+
export declare class ProfileBotHandle implements ProfileBot {
|
|
22
|
+
protected readonly scope: Scope;
|
|
23
|
+
protected readonly host: HandleHost;
|
|
24
|
+
readonly id: string;
|
|
25
|
+
readonly plugins: BotPlugins;
|
|
26
|
+
constructor(scope: Scope, host: HandleHost, id: string);
|
|
27
|
+
private get info();
|
|
28
|
+
get name(): string;
|
|
29
|
+
get uuid(): string;
|
|
30
|
+
get connected(): boolean;
|
|
31
|
+
get enabled(): boolean;
|
|
32
|
+
connect(): Promise<Result<any>>;
|
|
33
|
+
disconnect(): Promise<Result<any>>;
|
|
34
|
+
reconnect(options?: {
|
|
35
|
+
after?: Duration;
|
|
36
|
+
}): Promise<Result<any>>;
|
|
37
|
+
toString(): string;
|
|
38
|
+
}
|
|
39
|
+
export declare class BotHandle extends ProfileBotHandle implements Bot {
|
|
40
|
+
readonly version: string;
|
|
41
|
+
readonly world: World;
|
|
42
|
+
readonly entities: Entities;
|
|
43
|
+
readonly inventory: Inventory;
|
|
44
|
+
readonly containers: Containers;
|
|
45
|
+
constructor(scope: Scope, host: HandleHost, id: string, version: string);
|
|
46
|
+
craft(item: Item, options?: {
|
|
47
|
+
count?: number;
|
|
48
|
+
table?: Vec3;
|
|
49
|
+
}): Promise<Result<CraftFailure, {
|
|
50
|
+
crafted: number;
|
|
51
|
+
}>>;
|
|
52
|
+
recipes(item: Item): Promise<Recipe[]>;
|
|
53
|
+
private call;
|
|
54
|
+
on<E extends BotEvent>(event: E, listener: Listener<BotEventData[E]>): () => void;
|
|
55
|
+
status(): Promise<BotStatus>;
|
|
56
|
+
position(): Promise<Position>;
|
|
57
|
+
supports(id: Block | Item | Entity): Promise<boolean>;
|
|
58
|
+
goto(target: Vec3, options?: {
|
|
59
|
+
within?: number;
|
|
60
|
+
timeout?: Duration;
|
|
61
|
+
}): Promise<Result<any>>;
|
|
62
|
+
follow(entity: EntityInfo | number, options?: {
|
|
63
|
+
distance?: number;
|
|
64
|
+
timeout?: Duration;
|
|
65
|
+
}): Promise<Result<any>>;
|
|
66
|
+
stopMoving(): Promise<void>;
|
|
67
|
+
lookAt(target: Vec3): Promise<void>;
|
|
68
|
+
jump(): Promise<void>;
|
|
69
|
+
dig(position: Vec3): Promise<Result<any>>;
|
|
70
|
+
place(position: Vec3, item: Item): Promise<Result<any>>;
|
|
71
|
+
activateBlock(position: Vec3): Promise<Result<any>>;
|
|
72
|
+
useItem(): Promise<Result<any>>;
|
|
73
|
+
attack(entity: EntityInfo | number): Promise<Result<any>>;
|
|
74
|
+
say(message: string): Promise<void>;
|
|
75
|
+
command(command: string): Promise<void>;
|
|
76
|
+
wait(duration: Duration): Promise<void>;
|
|
77
|
+
loop(body: () => unknown, options?: {
|
|
78
|
+
every?: Duration;
|
|
79
|
+
}): Promise<never>;
|
|
80
|
+
}
|
|
81
|
+
export declare class ProfileHandle implements Profile {
|
|
82
|
+
private readonly scope;
|
|
83
|
+
private readonly host;
|
|
84
|
+
readonly id: string;
|
|
85
|
+
readonly name: string;
|
|
86
|
+
private readonly list;
|
|
87
|
+
constructor(scope: Scope, host: HandleHost, id: string, name: string, list: () => readonly BotInfo[]);
|
|
88
|
+
bots(): readonly ProfileBot[];
|
|
89
|
+
bot(idNameOrUuid: string): ProfileBot | undefined;
|
|
90
|
+
on<E extends ProfileEvent>(event: E, listener: Listener<ProfileEventData[E]>): () => void;
|
|
91
|
+
wait(duration: Duration): Promise<void>;
|
|
92
|
+
loop(body: () => unknown, options?: {
|
|
93
|
+
every?: Duration;
|
|
94
|
+
}): Promise<never>;
|
|
95
|
+
}
|