@minecraftbot-com/plugin 0.0.0-stage → 0.1.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 +177 -0
- package/README.md +45 -2
- package/dist/api.d.ts +351 -0
- package/dist/api.js +2 -0
- package/dist/cli/api.d.ts +26 -0
- package/dist/cli/api.js +61 -0
- package/dist/cli/build.d.ts +23 -0
- package/dist/cli/build.js +65 -0
- package/dist/cli/check.d.ts +10 -0
- package/dist/cli/check.js +98 -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/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 +112 -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/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 +55 -4
- package/src/api.ts +334 -0
- package/src/cli/api.ts +79 -0
- package/src/cli/build.ts +76 -0
- package/src/cli/check.ts +103 -0
- package/src/cli/config.ts +48 -0
- package/src/cli/create.ts +38 -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 +113 -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/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 +31 -0
- package/templates/basic/README.md +13 -0
- package/templates/basic/gitignore +3 -0
- package/templates/basic/package.json +18 -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,7 @@
|
|
|
1
|
+
import { BotEvent, Permission } from "./contracts.js";
|
|
2
|
+
/** Every operation the bot container accepts and the permission it needs (null: always allowed). For the test kit and `minebot check`; not part of the runtime. */
|
|
3
|
+
export declare const OPERATIONS: Readonly<Record<string, Permission | null>>;
|
|
4
|
+
/** Operations on any bot of the profile, not only those the plugin runs on. */
|
|
5
|
+
export declare const PROFILE_WIDE_OPERATIONS: ReadonlySet<string>;
|
|
6
|
+
/** The permission a subscription needs (null: always allowed). */
|
|
7
|
+
export declare const EVENT_PERMISSIONS: Readonly<Record<BotEvent, Permission | null>>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Generated from Minecraft 26.3 and the MineBot plugin protocol. Do not edit.
|
|
2
|
+
import { BotEvent, Permission } from "./contracts.js";
|
|
3
|
+
/** Every operation the bot container accepts and the permission it needs (null: always allowed). For the test kit and `minebot check`; not part of the runtime. */
|
|
4
|
+
export const OPERATIONS = {
|
|
5
|
+
"bot.activateBlock": Permission.Interact,
|
|
6
|
+
"bot.attack": Permission.Attack,
|
|
7
|
+
"bot.command": Permission.Commands,
|
|
8
|
+
"bot.connect": Permission.ProfileBots,
|
|
9
|
+
"bot.craft": Permission.InventoryManage,
|
|
10
|
+
"bot.dig": Permission.Interact,
|
|
11
|
+
"bot.disconnect": Permission.ProfileBots,
|
|
12
|
+
"bot.follow": Permission.Move,
|
|
13
|
+
"bot.goto": Permission.Move,
|
|
14
|
+
"bot.jump": Permission.Move,
|
|
15
|
+
"bot.lookAt": Permission.Move,
|
|
16
|
+
"bot.place": Permission.Interact,
|
|
17
|
+
"bot.position": Permission.WorldRead,
|
|
18
|
+
"bot.recipes": null,
|
|
19
|
+
"bot.reconnect": Permission.ProfileBots,
|
|
20
|
+
"bot.say": Permission.ChatSend,
|
|
21
|
+
"bot.status": null,
|
|
22
|
+
"bot.stopMoving": Permission.Move,
|
|
23
|
+
"bot.supports": null,
|
|
24
|
+
"bot.useItem": Permission.Interact,
|
|
25
|
+
"container.close": Permission.InventoryManage,
|
|
26
|
+
"container.deposit": Permission.InventoryManage,
|
|
27
|
+
"container.items": Permission.InventoryManage,
|
|
28
|
+
"container.open": Permission.InventoryManage,
|
|
29
|
+
"container.withdraw": Permission.InventoryManage,
|
|
30
|
+
"entities.nearby": Permission.EntitiesRead,
|
|
31
|
+
"entities.players": Permission.EntitiesRead,
|
|
32
|
+
"inventory.drop": Permission.InventoryManage,
|
|
33
|
+
"inventory.equip": Permission.InventoryManage,
|
|
34
|
+
"inventory.held": Permission.InventoryRead,
|
|
35
|
+
"inventory.items": Permission.InventoryRead,
|
|
36
|
+
"plugins.start": Permission.ProfilePlugins,
|
|
37
|
+
"plugins.stop": Permission.ProfilePlugins,
|
|
38
|
+
"storage.delete": null,
|
|
39
|
+
"storage.entries": null,
|
|
40
|
+
"storage.get": null,
|
|
41
|
+
"storage.set": null,
|
|
42
|
+
"world.block": Permission.WorldRead,
|
|
43
|
+
"world.findBlocks": Permission.WorldRead,
|
|
44
|
+
};
|
|
45
|
+
/** Operations on any bot of the profile, not only those the plugin runs on. */
|
|
46
|
+
export const PROFILE_WIDE_OPERATIONS = new Set([
|
|
47
|
+
"bot.connect",
|
|
48
|
+
"bot.disconnect",
|
|
49
|
+
"bot.reconnect",
|
|
50
|
+
"plugins.start",
|
|
51
|
+
"plugins.stop",
|
|
52
|
+
"storage.delete",
|
|
53
|
+
"storage.entries",
|
|
54
|
+
"storage.get",
|
|
55
|
+
"storage.set",
|
|
56
|
+
]);
|
|
57
|
+
/** The permission a subscription needs (null: always allowed). */
|
|
58
|
+
export const EVENT_PERMISSIONS = {
|
|
59
|
+
[BotEvent.Chat]: Permission.ChatRead,
|
|
60
|
+
[BotEvent.PlayerJoined]: Permission.EntitiesRead,
|
|
61
|
+
[BotEvent.PlayerLeft]: Permission.EntitiesRead,
|
|
62
|
+
[BotEvent.Died]: null,
|
|
63
|
+
[BotEvent.Respawned]: null,
|
|
64
|
+
[BotEvent.HealthChanged]: null,
|
|
65
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Generated from Minecraft 26.3 and the MineBot plugin protocol. Do not edit.
|
|
2
|
+
import { Permission } from "./contracts.js";
|
|
3
|
+
/** What the install dialog shows for each permission. Used by `minebot check`; not part of the runtime. */
|
|
4
|
+
export const PERMISSION_LABELS = {
|
|
5
|
+
[Permission.WorldRead]: "Can see the world around the bot",
|
|
6
|
+
[Permission.EntitiesRead]: "Can see nearby players and mobs",
|
|
7
|
+
[Permission.Move]: "Can move the bot",
|
|
8
|
+
[Permission.Interact]: "Can break, place and use blocks",
|
|
9
|
+
[Permission.Attack]: "Can attack players and mobs",
|
|
10
|
+
[Permission.InventoryRead]: "Can see the bot's inventory",
|
|
11
|
+
[Permission.InventoryManage]: "Can move, drop, equip and craft items, and use chests",
|
|
12
|
+
[Permission.ChatRead]: "Can read chat",
|
|
13
|
+
[Permission.ChatSend]: "Can send chat messages",
|
|
14
|
+
[Permission.Commands]: "Can run server commands (e.g. /pay, /tpa)",
|
|
15
|
+
[Permission.ProfileBots]: "Can control every bot in this profile",
|
|
16
|
+
[Permission.ProfilePlugins]: "Can start and stop other plugins",
|
|
17
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { PluginRuntime, buildManifest, SDK_VERSION } from "../runtime/runtime.js";
|
|
2
|
+
export type { LogLevel, RuntimeOptions } from "../runtime/runtime.js";
|
|
3
|
+
export { Connection } from "../runtime/connection.js";
|
|
4
|
+
export type { CallOutcome, Transport } from "../runtime/connection.js";
|
|
5
|
+
export { memoryTransportPair } from "../runtime/memory-transport.js";
|
|
6
|
+
export { PROTOCOL_VERSION } from "../protocol.js";
|
|
7
|
+
export type * from "../protocol.js";
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// @minecraftbot-com/plugin/host: what runs a plugin (the minebot CLI, the hosted runner). Plugin code doesn't need it.
|
|
2
|
+
export { PluginRuntime, buildManifest, SDK_VERSION } from "../runtime/runtime.js";
|
|
3
|
+
export { Connection } from "../runtime/connection.js";
|
|
4
|
+
export { memoryTransportPair } from "../runtime/memory-transport.js";
|
|
5
|
+
export { PROTOCOL_VERSION } from "../protocol.js";
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export { definePlugin, isDefinedPlugin } from "./plugin.js";
|
|
2
|
+
export type { DefinedPlugin, Logger, PluginDefinition, ProfileRunContext, RunContext, Stats, Warnings } from "./plugin.js";
|
|
3
|
+
export { Setting } from "./settings.js";
|
|
4
|
+
export type { SettingDefinition, SettingValues } from "./settings.js";
|
|
5
|
+
export { Storage } from "./storage.js";
|
|
6
|
+
export type { BotStorage, PerBot, ProfileStorage, StorageDefinition, StoredMap, StoredValue } from "./storage.js";
|
|
7
|
+
export type { Bot, BlockInfo, BlockStateFilter, BotEventData, BotPlugins, BotStatus, ChatMessage, Container, Containers, Entities, EntityInfo, Inventory, ItemStack, Listener, PlayerInfo, Profile, ProfileBot, ProfileEventData, Recipe, World, } from "./api.js";
|
|
8
|
+
export { Position, vec3, toMilliseconds } from "./types.js";
|
|
9
|
+
export type { Duration, JsonValue, Result, Vec3 } from "./types.js";
|
|
10
|
+
export { PluginError, PermissionDeniedError, StoppedError, isStopped } from "./errors.js";
|
|
11
|
+
export { network } from "./permissions.js";
|
|
12
|
+
export type { NetworkPermission } from "./permissions.js";
|
|
13
|
+
export { Block } from "./generated/blocks.js";
|
|
14
|
+
export { Item } from "./generated/items.js";
|
|
15
|
+
export { Entity } from "./generated/entities.js";
|
|
16
|
+
export { Effect } from "./generated/effects.js";
|
|
17
|
+
export type { BlockStates } from "./generated/block-states.js";
|
|
18
|
+
export { GAME_VERSION } from "./generated/version.js";
|
|
19
|
+
export { AttackFailure, BotControlFailure, BotEvent, BuiltInPlugin, ContainerFailure, ControlUnit, CraftFailure, DigFailure, EquipmentSlot, ErrorCode, GameMode, InteractFailure, InventoryFailure, MoveFailure, Permission, PlaceFailure, PluginControlFailure, PluginMode, ProfileEvent, RejectReason, StopReason, StorageScope, } from "./generated/contracts.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// @minecraftbot-com/plugin: write MineBot plugins in TypeScript. Start with `definePlugin`; see AGENTS.md for a tour.
|
|
2
|
+
export { definePlugin, isDefinedPlugin } from "./plugin.js";
|
|
3
|
+
export { Setting } from "./settings.js";
|
|
4
|
+
export { Storage } from "./storage.js";
|
|
5
|
+
export { Position, vec3, toMilliseconds } from "./types.js";
|
|
6
|
+
export { PluginError, PermissionDeniedError, StoppedError, isStopped } from "./errors.js";
|
|
7
|
+
export { network } from "./permissions.js";
|
|
8
|
+
export { Block } from "./generated/blocks.js";
|
|
9
|
+
export { Item } from "./generated/items.js";
|
|
10
|
+
export { Entity } from "./generated/entities.js";
|
|
11
|
+
export { Effect } from "./generated/effects.js";
|
|
12
|
+
export { GAME_VERSION } from "./generated/version.js";
|
|
13
|
+
export { AttackFailure, BotControlFailure, BotEvent, BuiltInPlugin, ContainerFailure, ControlUnit, CraftFailure, DigFailure, EquipmentSlot, ErrorCode, GameMode, InteractFailure, InventoryFailure, MoveFailure, Permission, PlaceFailure, PluginControlFailure, PluginMode, ProfileEvent, RejectReason, StopReason, StorageScope, } from "./generated/contracts.js";
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** A permission to `fetch` one host: build it with `network("api.example.com")`. */
|
|
2
|
+
export type NetworkPermission = `network:${string}`;
|
|
3
|
+
/**
|
|
4
|
+
* Lets the plugin `fetch` a host over HTTPS. `"*.example.com"` covers every subdomain (shown to the user as
|
|
5
|
+
* such); a bare `*` is never allowed. Add a port when it isn't 443: `"example.com:8443"`.
|
|
6
|
+
*
|
|
7
|
+
* @example permissions: [Permission.ChatRead, network("api.example.com")]
|
|
8
|
+
*/
|
|
9
|
+
export declare function network(host: string): NetworkPermission;
|
|
10
|
+
/** The host part of a network permission, or null for any other permission. */
|
|
11
|
+
export declare function networkHost(permission: string): string | null;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
const HOST = /^(\*\.)?([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z]{2,63}(:\d{1,5})?$/;
|
|
2
|
+
/**
|
|
3
|
+
* Lets the plugin `fetch` a host over HTTPS. `"*.example.com"` covers every subdomain (shown to the user as
|
|
4
|
+
* such); a bare `*` is never allowed. Add a port when it isn't 443: `"example.com:8443"`.
|
|
5
|
+
*
|
|
6
|
+
* @example permissions: [Permission.ChatRead, network("api.example.com")]
|
|
7
|
+
*/
|
|
8
|
+
export function network(host) {
|
|
9
|
+
const h = host.trim().toLowerCase();
|
|
10
|
+
if (!HOST.test(h)) {
|
|
11
|
+
throw new TypeError(`network("${host}"): expected a host name such as "api.example.com" or "*.example.com", without https:// or a path`);
|
|
12
|
+
}
|
|
13
|
+
return `network:${h}`;
|
|
14
|
+
}
|
|
15
|
+
/** The host part of a network permission, or null for any other permission. */
|
|
16
|
+
export function networkHost(permission) {
|
|
17
|
+
return permission.startsWith("network:") ? permission.slice("network:".length) : null;
|
|
18
|
+
}
|
package/dist/plugin.d.ts
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import type { ControlUnit, Permission } from "./generated/contracts.js";
|
|
2
|
+
import type { SettingDefinition, SettingValues } from "./settings.js";
|
|
3
|
+
import type { BotStorage, ProfileStorage, StorageDefinition } from "./storage.js";
|
|
4
|
+
import type { Bot, Profile } from "./api.js";
|
|
5
|
+
import type { NetworkPermission } from "./permissions.js";
|
|
6
|
+
/** Writes to the plugin console on the dashboard (and your terminal under `minebot dev`). */
|
|
7
|
+
export interface Logger {
|
|
8
|
+
debug(message: string): void;
|
|
9
|
+
info(message: string): void;
|
|
10
|
+
warn(message: string): void;
|
|
11
|
+
error(message: string | Error): void;
|
|
12
|
+
}
|
|
13
|
+
/** Numbers shown on the plugin's card on the dashboard. */
|
|
14
|
+
export interface Stats {
|
|
15
|
+
/** Sets a statistic, e.g. `stats.set("Crops harvested", 41)`. */
|
|
16
|
+
set(name: string, value: number): void;
|
|
17
|
+
/** Adds to a statistic (starting from 0). */
|
|
18
|
+
add(name: string, amount?: number): void;
|
|
19
|
+
}
|
|
20
|
+
/** Problems shown on the plugin's card until cleared, e.g. "No hoe in the inventory". */
|
|
21
|
+
export interface Warnings {
|
|
22
|
+
set(key: string, message: string): void;
|
|
23
|
+
clear(key: string): void;
|
|
24
|
+
}
|
|
25
|
+
export interface RunContext<S, T> {
|
|
26
|
+
/** The values the user set for this bot. */
|
|
27
|
+
readonly settings: SettingValues<S>;
|
|
28
|
+
/** Stored data; bot-scoped entries are bound to this bot. */
|
|
29
|
+
readonly storage: BotStorage<T>;
|
|
30
|
+
readonly log: Logger;
|
|
31
|
+
readonly stats: Stats;
|
|
32
|
+
readonly warnings: Warnings;
|
|
33
|
+
}
|
|
34
|
+
export interface ProfileRunContext<S, T> {
|
|
35
|
+
/** The values the user set for the plugin on this profile. */
|
|
36
|
+
readonly settings: SettingValues<S>;
|
|
37
|
+
/** Stored data; bot-scoped entries need `.for(bot)`. */
|
|
38
|
+
readonly storage: ProfileStorage<T>;
|
|
39
|
+
readonly log: Logger;
|
|
40
|
+
readonly stats: Stats;
|
|
41
|
+
readonly warnings: Warnings;
|
|
42
|
+
}
|
|
43
|
+
type AnySettings = Record<string, SettingDefinition<unknown>>;
|
|
44
|
+
type AnyStorage = Record<string, StorageDefinition<any, "value" | "map", any>>;
|
|
45
|
+
export interface PluginDefinition<S extends AnySettings = {}, T extends AnyStorage = {}> {
|
|
46
|
+
/** Shown on the dashboard. */
|
|
47
|
+
name: string;
|
|
48
|
+
/** What your plugin is allowed to do. Calls outside this list fail with a message naming the permission. */
|
|
49
|
+
permissions: readonly (Permission | NetworkPermission)[];
|
|
50
|
+
/**
|
|
51
|
+
* Parts of the bot only this plugin may drive while it runs. Other plugins (and macros) that need them
|
|
52
|
+
* wait. Declare `ControlUnit.Movement` if you walk, `ControlUnit.Look` if you aim, `ControlUnit.Hand` if
|
|
53
|
+
* you dig, place or attack.
|
|
54
|
+
*/
|
|
55
|
+
exclusive?: readonly ControlUnit[];
|
|
56
|
+
/** What the user can configure per bot. */
|
|
57
|
+
settings?: S;
|
|
58
|
+
/** Data kept between runs. */
|
|
59
|
+
storage?: T;
|
|
60
|
+
/** Runs once per bot the plugin is enabled on. Return (or throw) to finish; stopping the bot ends it. */
|
|
61
|
+
run?(bot: Bot, context: RunContext<S, T>): Promise<void>;
|
|
62
|
+
/** Runs once per profile while the plugin is installed and the profile runs. */
|
|
63
|
+
runProfile?(profile: Profile, context: ProfileRunContext<S, T>): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
/** What `definePlugin` returns: the definition, marked for the CLI and the runtime. */
|
|
66
|
+
export interface DefinedPlugin<S extends AnySettings = AnySettings, T extends AnyStorage = AnyStorage> {
|
|
67
|
+
readonly __minebotPlugin: 1;
|
|
68
|
+
readonly definition: PluginDefinition<S, T>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Declares a plugin. Export the result as your module's default export.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* export default definePlugin({
|
|
75
|
+
* name: "Greeter",
|
|
76
|
+
* permissions: [Permission.ChatRead, Permission.ChatSend],
|
|
77
|
+
* async run(bot) {
|
|
78
|
+
* bot.on(BotEvent.PlayerJoined, async ({ player }) => {
|
|
79
|
+
* await bot.say(`Welcome, ${player.name}!`);
|
|
80
|
+
* });
|
|
81
|
+
* },
|
|
82
|
+
* });
|
|
83
|
+
*/
|
|
84
|
+
export declare function definePlugin<const S extends AnySettings = {}, const T extends AnyStorage = {}>(definition: PluginDefinition<S, T>): DefinedPlugin<S, T>;
|
|
85
|
+
export declare function isDefinedPlugin(value: unknown): value is DefinedPlugin;
|
|
86
|
+
export {};
|
package/dist/plugin.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Declares a plugin. Export the result as your module's default export.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* export default definePlugin({
|
|
6
|
+
* name: "Greeter",
|
|
7
|
+
* permissions: [Permission.ChatRead, Permission.ChatSend],
|
|
8
|
+
* async run(bot) {
|
|
9
|
+
* bot.on(BotEvent.PlayerJoined, async ({ player }) => {
|
|
10
|
+
* await bot.say(`Welcome, ${player.name}!`);
|
|
11
|
+
* });
|
|
12
|
+
* },
|
|
13
|
+
* });
|
|
14
|
+
*/
|
|
15
|
+
export function definePlugin(definition) {
|
|
16
|
+
if (!definition || typeof definition.name !== "string" || definition.name.trim() === "") {
|
|
17
|
+
throw new TypeError("definePlugin needs a name, e.g. definePlugin({ name: \"Harvester\", ... })");
|
|
18
|
+
}
|
|
19
|
+
if (!definition.run && !definition.runProfile) {
|
|
20
|
+
throw new TypeError(`Plugin "${definition.name}" needs run(bot) or runProfile(profile)`);
|
|
21
|
+
}
|
|
22
|
+
return { __minebotPlugin: 1, definition };
|
|
23
|
+
}
|
|
24
|
+
export function isDefinedPlugin(value) {
|
|
25
|
+
return typeof value === "object" && value !== null && value.__minebotPlugin === 1;
|
|
26
|
+
}
|
|
@@ -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
|
+
}
|