@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.
Files changed (137) hide show
  1. package/AGENTS.md +188 -0
  2. package/README.md +47 -3
  3. package/dist/api.d.ts +354 -0
  4. package/dist/api.js +2 -0
  5. package/dist/cli/api.d.ts +49 -0
  6. package/dist/cli/api.js +69 -0
  7. package/dist/cli/build.d.ts +24 -0
  8. package/dist/cli/build.js +72 -0
  9. package/dist/cli/check.d.ts +17 -0
  10. package/dist/cli/check.js +99 -0
  11. package/dist/cli/config.d.ts +11 -0
  12. package/dist/cli/config.js +38 -0
  13. package/dist/cli/create.d.ts +2 -0
  14. package/dist/cli/create.js +39 -0
  15. package/dist/cli/deploy.d.ts +13 -0
  16. package/dist/cli/deploy.js +51 -0
  17. package/dist/cli/dev-mcp.d.ts +36 -0
  18. package/dist/cli/dev-mcp.js +101 -0
  19. package/dist/cli/dev.d.ts +15 -0
  20. package/dist/cli/dev.js +261 -0
  21. package/dist/cli/fetch-guard.d.ts +7 -0
  22. package/dist/cli/fetch-guard.js +66 -0
  23. package/dist/cli/main.d.ts +2 -0
  24. package/dist/cli/main.js +119 -0
  25. package/dist/cli/mcp.d.ts +24 -0
  26. package/dist/cli/mcp.js +136 -0
  27. package/dist/cli/output.d.ts +11 -0
  28. package/dist/cli/output.js +25 -0
  29. package/dist/cli/test.d.ts +2 -0
  30. package/dist/cli/test.js +39 -0
  31. package/dist/errors.d.ts +22 -0
  32. package/dist/errors.js +35 -0
  33. package/dist/generated/block-states.d.ts +4087 -0
  34. package/dist/generated/block-states.js +2 -0
  35. package/dist/generated/blocks.d.ts +1289 -0
  36. package/dist/generated/blocks.js +1291 -0
  37. package/dist/generated/contracts.d.ts +304 -0
  38. package/dist/generated/contracts.js +327 -0
  39. package/dist/generated/effects.d.ts +43 -0
  40. package/dist/generated/effects.js +45 -0
  41. package/dist/generated/entities.d.ts +164 -0
  42. package/dist/generated/entities.js +166 -0
  43. package/dist/generated/items.d.ts +1661 -0
  44. package/dist/generated/items.js +1663 -0
  45. package/dist/generated/operations.d.ts +7 -0
  46. package/dist/generated/operations.js +65 -0
  47. package/dist/generated/permission-labels.d.ts +3 -0
  48. package/dist/generated/permission-labels.js +17 -0
  49. package/dist/generated/version.d.ts +2 -0
  50. package/dist/generated/version.js +3 -0
  51. package/dist/host/index.d.ts +7 -0
  52. package/dist/host/index.js +5 -0
  53. package/dist/hosted/fetch.d.ts +53 -0
  54. package/dist/hosted/fetch.js +216 -0
  55. package/dist/hosted/index.d.ts +3 -0
  56. package/dist/hosted/index.js +81 -0
  57. package/dist/index.d.ts +19 -0
  58. package/dist/index.js +13 -0
  59. package/dist/permissions.d.ts +11 -0
  60. package/dist/permissions.js +18 -0
  61. package/dist/plugin.d.ts +86 -0
  62. package/dist/plugin.js +26 -0
  63. package/dist/protocol.d.ts +167 -0
  64. package/dist/protocol.js +4 -0
  65. package/dist/runtime/connection.d.ts +52 -0
  66. package/dist/runtime/connection.js +132 -0
  67. package/dist/runtime/handles.d.ts +95 -0
  68. package/dist/runtime/handles.js +289 -0
  69. package/dist/runtime/memory-transport.d.ts +6 -0
  70. package/dist/runtime/memory-transport.js +39 -0
  71. package/dist/runtime/runtime.d.ts +57 -0
  72. package/dist/runtime/runtime.js +265 -0
  73. package/dist/runtime/scope.d.ts +25 -0
  74. package/dist/runtime/scope.js +94 -0
  75. package/dist/runtime/storage-runtime.d.ts +10 -0
  76. package/dist/runtime/storage-runtime.js +131 -0
  77. package/dist/settings.d.ts +111 -0
  78. package/dist/settings.js +78 -0
  79. package/dist/storage.d.ts +78 -0
  80. package/dist/storage.js +24 -0
  81. package/dist/testing/fake-container.d.ts +175 -0
  82. package/dist/testing/fake-container.js +483 -0
  83. package/dist/testing/index.d.ts +37 -0
  84. package/dist/testing/index.js +54 -0
  85. package/dist/types.d.ts +65 -0
  86. package/dist/types.js +54 -0
  87. package/llms.txt +11 -0
  88. package/package.json +60 -4
  89. package/src/api.ts +337 -0
  90. package/src/cli/api.ts +105 -0
  91. package/src/cli/build.ts +83 -0
  92. package/src/cli/check.ts +104 -0
  93. package/src/cli/config.ts +48 -0
  94. package/src/cli/create.ts +38 -0
  95. package/src/cli/deploy.ts +60 -0
  96. package/src/cli/dev-mcp.ts +131 -0
  97. package/src/cli/dev.ts +272 -0
  98. package/src/cli/fetch-guard.ts +61 -0
  99. package/src/cli/main.ts +120 -0
  100. package/src/cli/mcp.ts +168 -0
  101. package/src/cli/output.ts +30 -0
  102. package/src/cli/test.ts +38 -0
  103. package/src/errors.ts +34 -0
  104. package/src/generated/block-states.ts +892 -0
  105. package/src/generated/blocks.ts +1291 -0
  106. package/src/generated/contracts.ts +329 -0
  107. package/src/generated/effects.ts +45 -0
  108. package/src/generated/entities.ts +166 -0
  109. package/src/generated/items.ts +1663 -0
  110. package/src/generated/operations.ts +69 -0
  111. package/src/generated/permission-labels.ts +19 -0
  112. package/src/generated/version.ts +3 -0
  113. package/src/host/index.ts +9 -0
  114. package/src/hosted/fetch.ts +224 -0
  115. package/src/hosted/index.ts +121 -0
  116. package/src/index.ts +34 -0
  117. package/src/permissions.ts +23 -0
  118. package/src/plugin.ts +105 -0
  119. package/src/protocol.ts +107 -0
  120. package/src/runtime/connection.ts +167 -0
  121. package/src/runtime/handles.ts +337 -0
  122. package/src/runtime/memory-transport.ts +41 -0
  123. package/src/runtime/runtime.ts +281 -0
  124. package/src/runtime/scope.ts +101 -0
  125. package/src/runtime/storage-runtime.ts +138 -0
  126. package/src/settings.ts +118 -0
  127. package/src/storage.ts +82 -0
  128. package/src/testing/fake-container.ts +571 -0
  129. package/src/testing/index.ts +70 -0
  130. package/src/types.ts +88 -0
  131. package/templates/basic/AGENTS.md +32 -0
  132. package/templates/basic/README.md +14 -0
  133. package/templates/basic/gitignore +3 -0
  134. package/templates/basic/package.json +19 -0
  135. package/templates/basic/src/index.ts +20 -0
  136. package/templates/basic/test/plugin.test.ts +14 -0
  137. package/templates/basic/tsconfig.json +14 -0
@@ -0,0 +1,94 @@
1
+ import { StoppedError } from "../errors.js";
2
+ /**
3
+ * A lifetime: one run(bot), one runProfile, or the whole plugin. Everything started inside it (calls,
4
+ * listeners, timers, child scopes) ends when it stops, so plugin code never needs cancellation plumbing.
5
+ */
6
+ export class Scope {
7
+ parent;
8
+ cleanups = new Set();
9
+ children = new Set();
10
+ stoppedReason = null;
11
+ constructor(parent = null) {
12
+ this.parent = parent;
13
+ parent?.children.add(this);
14
+ }
15
+ get stopped() {
16
+ return this.stoppedReason !== null;
17
+ }
18
+ get reason() {
19
+ return this.stoppedReason;
20
+ }
21
+ /** Throws StoppedError if this scope has stopped. */
22
+ check() {
23
+ if (this.stoppedReason !== null)
24
+ throw new StoppedError(this.stoppedReason);
25
+ }
26
+ child() {
27
+ this.check();
28
+ return new Scope(this);
29
+ }
30
+ /** Runs `cleanup` when the scope stops. Returns a function that runs it early and forgets it. */
31
+ onStop(cleanup) {
32
+ if (this.stoppedReason !== null) {
33
+ cleanup();
34
+ return () => { };
35
+ }
36
+ this.cleanups.add(cleanup);
37
+ return () => {
38
+ if (this.cleanups.delete(cleanup))
39
+ cleanup();
40
+ };
41
+ }
42
+ /** Forgets a cleanup without running it. */
43
+ forget(cleanup) {
44
+ this.cleanups.delete(cleanup);
45
+ }
46
+ stop(reason) {
47
+ if (this.stoppedReason !== null)
48
+ return;
49
+ this.stoppedReason = reason;
50
+ for (const child of [...this.children])
51
+ child.stop(reason);
52
+ this.children.clear();
53
+ for (const cleanup of [...this.cleanups]) {
54
+ try {
55
+ cleanup();
56
+ }
57
+ catch {
58
+ // A cleanup must not keep the others from running.
59
+ }
60
+ }
61
+ this.cleanups.clear();
62
+ this.parent?.children.delete(this);
63
+ }
64
+ /** A promise that settles like `promise` but rejects with StoppedError as soon as the scope stops. */
65
+ guard(promise) {
66
+ this.check();
67
+ return new Promise((resolve, reject) => {
68
+ const cancel = () => reject(new StoppedError(this.stoppedReason ?? "Stopped"));
69
+ this.cleanups.add(cancel);
70
+ promise.then(value => {
71
+ this.cleanups.delete(cancel);
72
+ resolve(value);
73
+ }, error => {
74
+ this.cleanups.delete(cancel);
75
+ reject(error);
76
+ });
77
+ });
78
+ }
79
+ /** Waits `ms`, or rejects with StoppedError when the scope stops first. */
80
+ sleep(ms) {
81
+ this.check();
82
+ return new Promise((resolve, reject) => {
83
+ const timer = setTimeout(() => {
84
+ this.cleanups.delete(cancel);
85
+ resolve();
86
+ }, ms);
87
+ const cancel = () => {
88
+ clearTimeout(timer);
89
+ reject(new StoppedError(this.stoppedReason ?? "Stopped"));
90
+ };
91
+ this.cleanups.add(cancel);
92
+ });
93
+ }
94
+ }
@@ -0,0 +1,10 @@
1
+ import type { StorageSchema } from "../protocol.js";
2
+ import type { CallOutcome } from "./connection.js";
3
+ import type { Scope } from "./scope.js";
4
+ type Call = (scope: Scope, op: string, args: Record<string, unknown>) => Promise<CallOutcome>;
5
+ /**
6
+ * Builds the `storage` object for a scope from the manifest's declarations. In run(bot) `bot` is that bot's
7
+ * uuid and bot-scoped entries bind to it; in runProfile `bot` is null and bot-scoped entries need `.for(bot)`.
8
+ */
9
+ export declare function createStorage(declared: Record<string, StorageSchema>, call: Call, scope: Scope, bot: string | null): Record<string, unknown>;
10
+ export {};
@@ -0,0 +1,131 @@
1
+ import { PluginError } from "../errors.js";
2
+ import { StorageScope } from "../generated/contracts.js";
3
+ const MAX_UPDATE_ATTEMPTS = 16;
4
+ function ok(outcome) {
5
+ if (!outcome.ok)
6
+ throw new PluginError(outcome.failure, outcome.message);
7
+ return outcome.value;
8
+ }
9
+ function clone(value) {
10
+ return value === undefined ? value : JSON.parse(JSON.stringify(value));
11
+ }
12
+ class Value {
13
+ call;
14
+ scope;
15
+ at;
16
+ fallback;
17
+ constructor(call, scope, at, fallback) {
18
+ this.call = call;
19
+ this.scope = scope;
20
+ this.at = at;
21
+ this.fallback = fallback;
22
+ }
23
+ async read() {
24
+ const r = ok(await this.call(this.scope, "storage.get", { ...this.at, key: null }));
25
+ return { value: r.value === null || r.value === undefined ? clone(this.fallback) : r.value, version: r.version };
26
+ }
27
+ async get() {
28
+ return (await this.read()).value;
29
+ }
30
+ async set(value) {
31
+ ok(await this.call(this.scope, "storage.set", { ...this.at, key: null, value, ifVersion: null }));
32
+ }
33
+ async delete() {
34
+ ok(await this.call(this.scope, "storage.delete", { ...this.at, key: null, ifVersion: null }));
35
+ }
36
+ async update(fn) {
37
+ for (let attempt = 0; attempt < MAX_UPDATE_ATTEMPTS; attempt++) {
38
+ const { value, version } = await this.read();
39
+ const next = fn(value);
40
+ const outcome = await this.call(this.scope, "storage.set", { ...this.at, key: null, value: next, ifVersion: version });
41
+ if (outcome.ok)
42
+ return next;
43
+ if (outcome.failure !== "Conflict")
44
+ throw new PluginError(outcome.failure, outcome.message);
45
+ }
46
+ throw new PluginError("Conflict", `storage.${this.at.name}.update: gave up after ${MAX_UPDATE_ATTEMPTS} conflicting writes`);
47
+ }
48
+ }
49
+ class Map_ {
50
+ call;
51
+ scope;
52
+ at;
53
+ constructor(call, scope, at) {
54
+ this.call = call;
55
+ this.scope = scope;
56
+ this.at = at;
57
+ }
58
+ checkKey(key) {
59
+ if (typeof key !== "string" || key.length === 0 || key.length > 128)
60
+ throw new TypeError(`storage.${this.at.name}: keys are strings of 1 to 128 characters`);
61
+ }
62
+ async read(key) {
63
+ this.checkKey(key);
64
+ const r = ok(await this.call(this.scope, "storage.get", { ...this.at, key }));
65
+ return { value: r.value === null ? undefined : r.value, version: r.version };
66
+ }
67
+ async get(key) {
68
+ return (await this.read(key)).value;
69
+ }
70
+ async set(key, value) {
71
+ this.checkKey(key);
72
+ ok(await this.call(this.scope, "storage.set", { ...this.at, key, value, ifVersion: null }));
73
+ }
74
+ async delete(key) {
75
+ this.checkKey(key);
76
+ ok(await this.call(this.scope, "storage.delete", { ...this.at, key, ifVersion: null }));
77
+ }
78
+ async update(key, fn) {
79
+ for (let attempt = 0; attempt < MAX_UPDATE_ATTEMPTS; attempt++) {
80
+ const { value, version } = await this.read(key);
81
+ const next = fn(value);
82
+ const outcome = await this.call(this.scope, "storage.set", { ...this.at, key, value: next, ifVersion: version });
83
+ if (outcome.ok)
84
+ return next;
85
+ if (outcome.failure !== "Conflict")
86
+ throw new PluginError(outcome.failure, outcome.message);
87
+ }
88
+ throw new PluginError("Conflict", `storage.${this.at.name}.update: gave up after ${MAX_UPDATE_ATTEMPTS} conflicting writes`);
89
+ }
90
+ async keys() {
91
+ return (await this.entries()).map(([k]) => k);
92
+ }
93
+ async entries() {
94
+ const all = [];
95
+ let after = null;
96
+ for (;;) {
97
+ const page = ok(await this.call(this.scope, "storage.entries", { ...this.at, after, limit: 100 }));
98
+ all.push(...page.entries);
99
+ if (!page.next)
100
+ return all;
101
+ after = page.next;
102
+ }
103
+ }
104
+ }
105
+ /**
106
+ * Builds the `storage` object for a scope from the manifest's declarations. In run(bot) `bot` is that bot's
107
+ * uuid and bot-scoped entries bind to it; in runProfile `bot` is null and bot-scoped entries need `.for(bot)`.
108
+ */
109
+ export function createStorage(declared, call, scope, bot) {
110
+ const storage = {};
111
+ for (const [name, schema] of Object.entries(declared)) {
112
+ const make = (botUuid) => {
113
+ const at = { name, scope: schema.scope, bot: schema.scope === StorageScope.Bot ? botUuid : null };
114
+ return schema.kind === "map" ? new Map_(call, scope, at) : new Value(call, scope, at, schema.default);
115
+ };
116
+ if (schema.scope === StorageScope.Bot && bot === null) {
117
+ const perBot = {
118
+ for: target => {
119
+ if (!target?.uuid)
120
+ throw new TypeError(`storage.${name}.for(bot): pass a bot from profile.bots()`);
121
+ return make(target.uuid);
122
+ },
123
+ };
124
+ storage[name] = perBot;
125
+ }
126
+ else {
127
+ storage[name] = make(bot);
128
+ }
129
+ }
130
+ return storage;
131
+ }
@@ -0,0 +1,111 @@
1
+ import type { SettingSchema } from "./protocol.js";
2
+ import type { Block } from "./generated/blocks.js";
3
+ import type { Item } from "./generated/items.js";
4
+ import type { Entity } from "./generated/entities.js";
5
+ import type { Vec3 } from "./types.js";
6
+ /**
7
+ * One setting the user fills in on the dashboard. Build it with `Setting.*`; the type parameter is the
8
+ * value your plugin receives in `settings`.
9
+ */
10
+ export interface SettingDefinition<T> {
11
+ readonly schema: SettingSchema;
12
+ /** Type only, never set: carries the value type to `settings`. */
13
+ readonly __value?: T;
14
+ }
15
+ interface Common<T> {
16
+ /** Shown next to the field. */
17
+ label: string;
18
+ /** Shown under the field. */
19
+ description?: string;
20
+ default?: T;
21
+ }
22
+ type Optional<T, O> = O extends {
23
+ optional: true;
24
+ } ? T | undefined : T;
25
+ /**
26
+ * Settings the user configures on the dashboard, per bot.
27
+ *
28
+ * @example
29
+ * settings: {
30
+ * crop: Setting.block({ label: "Crop", options: [Block.Wheat, Block.Carrots], default: Block.Wheat }),
31
+ * radius: Setting.radius({ label: "Search radius", default: 16, max: 48 }),
32
+ * greeting: Setting.text({ label: "Greeting", default: "Hello!" }),
33
+ * }
34
+ */
35
+ export declare const Setting: {
36
+ /** Free text. `max` limits the length (default 256). */
37
+ text<O extends Common<string> & {
38
+ optional?: boolean;
39
+ max?: number;
40
+ }>(o: O): SettingDefinition<Optional<string, O>>;
41
+ /** A number between `min` and `max`. */
42
+ number<O extends Common<number> & {
43
+ optional?: boolean;
44
+ min?: number;
45
+ max?: number;
46
+ }>(o: O): SettingDefinition<Optional<number, O>>;
47
+ /** A distance in blocks, 1 to `max` (at most 64). */
48
+ radius(o: Common<number> & {
49
+ max?: number;
50
+ }): SettingDefinition<number>;
51
+ /** On or off. */
52
+ boolean(o: Common<boolean>): SettingDefinition<boolean>;
53
+ /** One of a fixed list of values, e.g. an enum's members. */
54
+ select<const V extends string | number, O extends Common<V> & {
55
+ options: readonly V[];
56
+ optional?: boolean;
57
+ }>(o: O): SettingDefinition<Optional<V, O>>;
58
+ /** A block type. `options` limits the choice; without it any block can be picked. */
59
+ block<O extends Common<Block> & {
60
+ options?: readonly Block[];
61
+ optional?: boolean;
62
+ }>(o: O): SettingDefinition<Optional<Block, O>>;
63
+ /** An item type. */
64
+ item<O extends Common<Item> & {
65
+ options?: readonly Item[];
66
+ optional?: boolean;
67
+ }>(o: O): SettingDefinition<Optional<Item, O>>;
68
+ /** An entity type (mob, animal, player...). */
69
+ entity<O extends Common<Entity> & {
70
+ options?: readonly Entity[];
71
+ optional?: boolean;
72
+ }>(o: O): SettingDefinition<Optional<Entity, O>>;
73
+ /** A position in the world (x, y, z). */
74
+ position<O extends Common<Vec3> & {
75
+ optional?: boolean;
76
+ }>(o: O): SettingDefinition<Optional<Vec3, O>>;
77
+ /**
78
+ * An HTTPS address the user enters, such as a webhook. Your plugin may `fetch` exactly this origin
79
+ * without declaring a network permission. `hosts` limits which hosts are accepted.
80
+ *
81
+ * @example
82
+ * webhook: Setting.url({ label: "Discord webhook", hosts: ["discord.com"], secret: true, optional: true })
83
+ */
84
+ url<O extends Common<string> & {
85
+ hosts?: readonly string[];
86
+ secret?: boolean;
87
+ optional?: boolean;
88
+ }>(o: O): SettingDefinition<Optional<string, O>>;
89
+ /**
90
+ * A secret such as an API key: stored encrypted, never shown again on the dashboard, sent only to the
91
+ * running plugin and never to its author.
92
+ */
93
+ secret<O extends {
94
+ label: string;
95
+ description?: string;
96
+ optional?: boolean;
97
+ }>(o: O): SettingDefinition<Optional<string, O>>;
98
+ /** A list of values of one kind, e.g. `Setting.list({ label: "Owners", of: Setting.text({ label: "Name" }) })`. */
99
+ list<T>(o: {
100
+ label: string;
101
+ description?: string;
102
+ of: SettingDefinition<T>;
103
+ max?: number;
104
+ default?: T[];
105
+ }): SettingDefinition<T[]>;
106
+ };
107
+ /** The values your plugin receives for a settings declaration. */
108
+ export type SettingValues<S> = {
109
+ readonly [K in keyof S]: S[K] extends SettingDefinition<infer T> ? T : never;
110
+ };
111
+ export {};
@@ -0,0 +1,78 @@
1
+ function define(schema) {
2
+ return { schema };
3
+ }
4
+ function clean(schema) {
5
+ for (const key of Object.keys(schema))
6
+ if (schema[key] === undefined)
7
+ delete schema[key];
8
+ return schema;
9
+ }
10
+ /**
11
+ * Settings the user configures on the dashboard, per bot.
12
+ *
13
+ * @example
14
+ * settings: {
15
+ * crop: Setting.block({ label: "Crop", options: [Block.Wheat, Block.Carrots], default: Block.Wheat }),
16
+ * radius: Setting.radius({ label: "Search radius", default: 16, max: 48 }),
17
+ * greeting: Setting.text({ label: "Greeting", default: "Hello!" }),
18
+ * }
19
+ */
20
+ export const Setting = {
21
+ /** Free text. `max` limits the length (default 256). */
22
+ text(o) {
23
+ return define(clean({ type: "text", label: o.label, description: o.description, default: o.default, optional: o.optional, max: o.max ?? 256 }));
24
+ },
25
+ /** A number between `min` and `max`. */
26
+ number(o) {
27
+ return define(clean({ type: "number", label: o.label, description: o.description, default: o.default, optional: o.optional, min: o.min, max: o.max }));
28
+ },
29
+ /** A distance in blocks, 1 to `max` (at most 64). */
30
+ radius(o) {
31
+ return define(clean({ type: "radius", label: o.label, description: o.description, default: o.default ?? 16, min: 1, max: Math.min(o.max ?? 32, 64) }));
32
+ },
33
+ /** On or off. */
34
+ boolean(o) {
35
+ return define(clean({ type: "boolean", label: o.label, description: o.description, default: o.default ?? false }));
36
+ },
37
+ /** One of a fixed list of values, e.g. an enum's members. */
38
+ select(o) {
39
+ return define(clean({ type: "select", label: o.label, description: o.description, default: o.default, optional: o.optional, options: [...o.options] }));
40
+ },
41
+ /** A block type. `options` limits the choice; without it any block can be picked. */
42
+ block(o) {
43
+ return define(clean({ type: "block", label: o.label, description: o.description, default: o.default, optional: o.optional, options: o.options ? [...o.options] : undefined }));
44
+ },
45
+ /** An item type. */
46
+ item(o) {
47
+ return define(clean({ type: "item", label: o.label, description: o.description, default: o.default, optional: o.optional, options: o.options ? [...o.options] : undefined }));
48
+ },
49
+ /** An entity type (mob, animal, player...). */
50
+ entity(o) {
51
+ return define(clean({ type: "entity", label: o.label, description: o.description, default: o.default, optional: o.optional, options: o.options ? [...o.options] : undefined }));
52
+ },
53
+ /** A position in the world (x, y, z). */
54
+ position(o) {
55
+ return define(clean({ type: "position", label: o.label, description: o.description, default: o.default, optional: o.optional }));
56
+ },
57
+ /**
58
+ * An HTTPS address the user enters, such as a webhook. Your plugin may `fetch` exactly this origin
59
+ * without declaring a network permission. `hosts` limits which hosts are accepted.
60
+ *
61
+ * @example
62
+ * webhook: Setting.url({ label: "Discord webhook", hosts: ["discord.com"], secret: true, optional: true })
63
+ */
64
+ url(o) {
65
+ return define(clean({ type: "url", label: o.label, description: o.description, default: o.secret ? undefined : o.default, optional: o.optional, hosts: o.hosts ? [...o.hosts] : undefined, secret: o.secret || undefined }));
66
+ },
67
+ /**
68
+ * A secret such as an API key: stored encrypted, never shown again on the dashboard, sent only to the
69
+ * running plugin and never to its author.
70
+ */
71
+ secret(o) {
72
+ return define(clean({ type: "secret", label: o.label, description: o.description, optional: o.optional, secret: true }));
73
+ },
74
+ /** A list of values of one kind, e.g. `Setting.list({ label: "Owners", of: Setting.text({ label: "Name" }) })`. */
75
+ list(o) {
76
+ return define(clean({ type: "list", label: o.label, description: o.description, of: o.of.schema, max: o.max ?? 32, default: o.default ?? [] }));
77
+ },
78
+ };
@@ -0,0 +1,78 @@
1
+ import type { StorageSchema } from "./protocol.js";
2
+ import { StorageScope } from "./generated/contracts.js";
3
+ import type { JsonValue } from "./types.js";
4
+ /** Declares one stored value. Build it with `Storage.value` or `Storage.map`. */
5
+ export interface StorageDefinition<T, K extends "value" | "map", S extends StorageScope> {
6
+ readonly schema: StorageSchema;
7
+ readonly __value?: T;
8
+ readonly __kind?: K;
9
+ readonly __scope?: S;
10
+ }
11
+ /**
12
+ * Data your plugin keeps between runs. It belongs to the user who installed the plugin, is limited to
13
+ * 1 MB per scope and 64 KB per value, and holds JSON values only.
14
+ *
15
+ * Scopes: `StorageScope.Profile` (default, shared by every bot of the profile), `StorageScope.Bot` (one per
16
+ * account, survives renames), `StorageScope.User` (shared by all of the user's profiles).
17
+ *
18
+ * @example
19
+ * storage: {
20
+ * harvested: Storage.value({ scope: StorageScope.Bot, default: 0 }),
21
+ * homes: Storage.map<Vec3>(),
22
+ * }
23
+ */
24
+ export declare const Storage: {
25
+ /** A single value. `get()` returns `default` until something is set. */
26
+ value<T, S extends StorageScope = StorageScope.Profile>(o: {
27
+ scope?: S;
28
+ default: T & JsonValue;
29
+ }): StorageDefinition<T, "value", NoInfer<S>>;
30
+ /** Keys (strings, up to 128 characters) to values. */
31
+ map<T extends JsonValue, S extends StorageScope = StorageScope.Profile>(o?: {
32
+ scope?: S;
33
+ }): StorageDefinition<T, "map", NoInfer<S>>;
34
+ };
35
+ /** A stored value. */
36
+ export interface StoredValue<T> {
37
+ /** The current value, or the declared default. */
38
+ get(): Promise<T>;
39
+ set(value: T): Promise<void>;
40
+ /** Back to the default. */
41
+ delete(): Promise<void>;
42
+ /**
43
+ * Read, change and write in one step. If another bot wrote in between, `fn` runs again on the newer
44
+ * value, so concurrent updates are never lost.
45
+ *
46
+ * @example await storage.harvested.update(n => n + 1);
47
+ */
48
+ update(fn: (current: T) => T): Promise<T>;
49
+ }
50
+ /** A stored map from string keys to values. */
51
+ export interface StoredMap<T> {
52
+ get(key: string): Promise<T | undefined>;
53
+ set(key: string, value: T): Promise<void>;
54
+ delete(key: string): Promise<void>;
55
+ /** Change one entry in one step; `fn` gets `undefined` for a missing key. */
56
+ update(key: string, fn: (current: T | undefined) => T): Promise<T>;
57
+ /** Every key, in order. */
58
+ keys(): Promise<string[]>;
59
+ /** Every entry, in key order. */
60
+ entries(): Promise<[string, T][]>;
61
+ }
62
+ /** A bot-scoped value seen from `runProfile`: pick the bot first. */
63
+ export interface PerBot<S> {
64
+ for(bot: {
65
+ readonly uuid: string;
66
+ }): S;
67
+ }
68
+ type Stored<T, K> = K extends "map" ? StoredMap<T> : StoredValue<T>;
69
+ type Accessor<D, InRun extends boolean> = D extends StorageDefinition<infer T, infer K, infer S> ? InRun extends true ? Stored<T, K> : S extends StorageScope.Bot ? PerBot<Stored<T, K>> : Stored<T, K> : never;
70
+ /** `storage` inside `run(bot)`: bot-scoped entries are already bound to that bot. */
71
+ export type BotStorage<S> = {
72
+ readonly [K in keyof S]: Accessor<S[K], true>;
73
+ };
74
+ /** `storage` inside `runProfile`: bot-scoped entries need `.for(bot)`. */
75
+ export type ProfileStorage<S> = {
76
+ readonly [K in keyof S]: Accessor<S[K], false>;
77
+ };
78
+ export {};
@@ -0,0 +1,24 @@
1
+ import { StorageScope } from "./generated/contracts.js";
2
+ /**
3
+ * Data your plugin keeps between runs. It belongs to the user who installed the plugin, is limited to
4
+ * 1 MB per scope and 64 KB per value, and holds JSON values only.
5
+ *
6
+ * Scopes: `StorageScope.Profile` (default, shared by every bot of the profile), `StorageScope.Bot` (one per
7
+ * account, survives renames), `StorageScope.User` (shared by all of the user's profiles).
8
+ *
9
+ * @example
10
+ * storage: {
11
+ * harvested: Storage.value({ scope: StorageScope.Bot, default: 0 }),
12
+ * homes: Storage.map<Vec3>(),
13
+ * }
14
+ */
15
+ export const Storage = {
16
+ /** A single value. `get()` returns `default` until something is set. */
17
+ value(o) {
18
+ return { schema: { kind: "value", scope: o.scope ?? StorageScope.Profile, default: o.default } };
19
+ },
20
+ /** Keys (strings, up to 128 characters) to values. */
21
+ map(o = {}) {
22
+ return { schema: { kind: "map", scope: o.scope ?? StorageScope.Profile } };
23
+ },
24
+ };