@mushinjs/plugin-bots 0.3.0 → 0.4.0-next.20261124175848
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/dist/bot.d.ts +30 -4
- package/dist/bot.js +90 -11
- package/dist/bot.js.map +1 -1
- package/dist/commands.d.ts +1 -1
- package/dist/commands.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/manager.d.ts +171 -11
- package/dist/manager.js +259 -21
- package/dist/manager.js.map +1 -1
- package/dist/media.d.ts +28 -0
- package/dist/media.js +91 -0
- package/dist/media.js.map +1 -0
- package/dist/types.d.ts +53 -0
- package/dist/types.js.map +1 -1
- package/package.json +4 -4
package/dist/media.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { readFile } from "node:fs/promises";
|
|
2
|
+
import { basename, extname } from "node:path";
|
|
3
|
+
// src/media.ts
|
|
4
|
+
const MIME = {
|
|
5
|
+
".png": "image/png",
|
|
6
|
+
".jpg": "image/jpeg",
|
|
7
|
+
".jpeg": "image/jpeg",
|
|
8
|
+
".gif": "image/gif",
|
|
9
|
+
".webp": "image/webp",
|
|
10
|
+
".bmp": "image/bmp",
|
|
11
|
+
".mp4": "video/mp4",
|
|
12
|
+
".mov": "video/quicktime",
|
|
13
|
+
".mp3": "audio/mpeg",
|
|
14
|
+
".ogg": "audio/ogg",
|
|
15
|
+
".oga": "audio/ogg",
|
|
16
|
+
".wav": "audio/wav",
|
|
17
|
+
".amr": "audio/amr",
|
|
18
|
+
".silk": "audio/silk",
|
|
19
|
+
".pdf": "application/pdf",
|
|
20
|
+
".txt": "text/plain",
|
|
21
|
+
".zip": "application/zip"
|
|
22
|
+
};
|
|
23
|
+
const DEFAULT_EXT = {
|
|
24
|
+
image: ".png",
|
|
25
|
+
record: ".mp3",
|
|
26
|
+
video: ".mp4",
|
|
27
|
+
file: ".bin"
|
|
28
|
+
};
|
|
29
|
+
/** A media type from a file name, or a fallback for the segment type. */
|
|
30
|
+
function mimeOf(name, type = "file") {
|
|
31
|
+
return MIME[extname(name).toLowerCase()] ?? (type === "image" ? "image/png" : type === "record" ? "audio/mpeg" : type === "video" ? "video/mp4" : "application/octet-stream");
|
|
32
|
+
}
|
|
33
|
+
/** Reads where a media segment's content is; local files and inline payloads are read into memory. */
|
|
34
|
+
async function mediaSource(segment) {
|
|
35
|
+
const value = String(segment.data.url || segment.data.file || segment.data.path || "");
|
|
36
|
+
const given = typeof segment.data.name === "string" && segment.data.name ? segment.data.name : void 0;
|
|
37
|
+
if (!value) throw new Error(`the ${segment.type} segment has no file or url`);
|
|
38
|
+
const fallbackName = given ?? `${segment.type}${DEFAULT_EXT[segment.type] ?? ".bin"}`;
|
|
39
|
+
if (/^https?:\/\//i.test(value)) return {
|
|
40
|
+
kind: "url",
|
|
41
|
+
url: value,
|
|
42
|
+
...given ? { name: given } : {}
|
|
43
|
+
};
|
|
44
|
+
if (value.startsWith("base64://")) return {
|
|
45
|
+
kind: "bytes",
|
|
46
|
+
bytes: Buffer.from(value.slice(9), "base64"),
|
|
47
|
+
name: fallbackName,
|
|
48
|
+
mime: mimeOf(fallbackName, segment.type)
|
|
49
|
+
};
|
|
50
|
+
const data = /^data:([^;,]+)?(;base64)?,(.*)$/s.exec(value);
|
|
51
|
+
if (data) return {
|
|
52
|
+
kind: "bytes",
|
|
53
|
+
bytes: data[2] ? Buffer.from(data[3], "base64") : Buffer.from(decodeURIComponent(data[3])),
|
|
54
|
+
name: fallbackName,
|
|
55
|
+
mime: data[1] || mimeOf(fallbackName, segment.type)
|
|
56
|
+
};
|
|
57
|
+
const path = value.replace(/^file:\/\/\/?/i, process.platform === "win32" ? "" : "/");
|
|
58
|
+
const name = given ?? basename(path);
|
|
59
|
+
return {
|
|
60
|
+
kind: "bytes",
|
|
61
|
+
bytes: await readFile(path),
|
|
62
|
+
name,
|
|
63
|
+
mime: mimeOf(name, segment.type)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/** The bytes of a media segment, downloading http(s) addresses; for platforms that only take uploads. */
|
|
67
|
+
async function mediaBytes(segment, timeout = 3e4) {
|
|
68
|
+
const source = await mediaSource(segment);
|
|
69
|
+
if (source.kind === "bytes") return source;
|
|
70
|
+
const response = await fetch(source.url, { signal: AbortSignal.timeout(timeout) });
|
|
71
|
+
if (!response.ok) throw new Error(`downloading ${source.url} failed: HTTP ${response.status}`);
|
|
72
|
+
const bytes = Buffer.from(await response.arrayBuffer());
|
|
73
|
+
const name = source.name ?? (basename(new URL(source.url).pathname) || `${segment.type}${DEFAULT_EXT[segment.type] ?? ".bin"}`);
|
|
74
|
+
const header = response.headers.get("content-type")?.split(";")[0];
|
|
75
|
+
return {
|
|
76
|
+
kind: "bytes",
|
|
77
|
+
bytes,
|
|
78
|
+
name,
|
|
79
|
+
mime: header && header !== "application/octet-stream" ? header : mimeOf(name, segment.type)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
/** The media segment types adapters send as attachments. */
|
|
83
|
+
const MEDIA_SEGMENTS = /* @__PURE__ */ new Set([
|
|
84
|
+
"image",
|
|
85
|
+
"record",
|
|
86
|
+
"video",
|
|
87
|
+
"file"
|
|
88
|
+
]);
|
|
89
|
+
export { MEDIA_SEGMENTS, mediaBytes, mediaSource, mimeOf };
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=media.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"media.js","names":[],"sources":["../src/media.ts"],"sourcesContent":["import { readFile } from 'node:fs/promises'\nimport { basename, extname } from 'node:path'\nimport type { Segment } from '@mushinjs/core'\n\n/**\n * Where an outgoing image, voice, video or file segment's bytes are. Segments follow OneBot, so\n * `file` (or `url`) holds an http(s) address, a `base64://` or `data:` payload, or a local path.\n */\nexport type MediaSource =\n | { kind: 'url'; url: string; name?: string }\n | { kind: 'bytes'; bytes: Buffer; name: string; mime: string }\n\nconst MIME: Record<string, string> = {\n '.png': 'image/png',\n '.jpg': 'image/jpeg',\n '.jpeg': 'image/jpeg',\n '.gif': 'image/gif',\n '.webp': 'image/webp',\n '.bmp': 'image/bmp',\n '.mp4': 'video/mp4',\n '.mov': 'video/quicktime',\n '.mp3': 'audio/mpeg',\n '.ogg': 'audio/ogg',\n '.oga': 'audio/ogg',\n '.wav': 'audio/wav',\n '.amr': 'audio/amr',\n '.silk': 'audio/silk',\n '.pdf': 'application/pdf',\n '.txt': 'text/plain',\n '.zip': 'application/zip',\n}\n\nconst DEFAULT_EXT: Record<string, string> = { image: '.png', record: '.mp3', video: '.mp4', file: '.bin' }\n\n/** A media type from a file name, or a fallback for the segment type. */\nexport function mimeOf(name: string, type = 'file') {\n return MIME[extname(name).toLowerCase()] ?? (type === 'image' ? 'image/png' : type === 'record' ? 'audio/mpeg' : type === 'video' ? 'video/mp4' : 'application/octet-stream')\n}\n\n/** Reads where a media segment's content is; local files and inline payloads are read into memory. */\nexport async function mediaSource(segment: Segment): Promise<MediaSource> {\n const value = String(segment.data.url || segment.data.file || segment.data.path || '')\n const given = typeof segment.data.name === 'string' && segment.data.name ? segment.data.name : undefined\n if (!value) throw new Error(`the ${segment.type} segment has no file or url`)\n const fallbackName = given ?? `${segment.type}${DEFAULT_EXT[segment.type] ?? '.bin'}`\n if (/^https?:\\/\\//i.test(value)) return { kind: 'url', url: value, ...(given ? { name: given } : {}) }\n if (value.startsWith('base64://')) {\n return { kind: 'bytes', bytes: Buffer.from(value.slice(9), 'base64'), name: fallbackName, mime: mimeOf(fallbackName, segment.type) }\n }\n const data = /^data:([^;,]+)?(;base64)?,(.*)$/s.exec(value)\n if (data) {\n const bytes = data[2] ? Buffer.from(data[3], 'base64') : Buffer.from(decodeURIComponent(data[3]))\n return { kind: 'bytes', bytes, name: fallbackName, mime: data[1] || mimeOf(fallbackName, segment.type) }\n }\n const path = value.replace(/^file:\\/\\/\\/?/i, process.platform === 'win32' ? '' : '/')\n const name = given ?? basename(path)\n return { kind: 'bytes', bytes: await readFile(path), name, mime: mimeOf(name, segment.type) }\n}\n\n/** The bytes of a media segment, downloading http(s) addresses; for platforms that only take uploads. */\nexport async function mediaBytes(segment: Segment, timeout = 30_000) {\n const source = await mediaSource(segment)\n if (source.kind === 'bytes') return source\n const response = await fetch(source.url, { signal: AbortSignal.timeout(timeout) })\n if (!response.ok) throw new Error(`downloading ${source.url} failed: HTTP ${response.status}`)\n const bytes = Buffer.from(await response.arrayBuffer())\n const name = source.name ?? (basename(new URL(source.url).pathname) || `${segment.type}${DEFAULT_EXT[segment.type] ?? '.bin'}`)\n const header = response.headers.get('content-type')?.split(';')[0]\n return { kind: 'bytes' as const, bytes, name, mime: header && header !== 'application/octet-stream' ? header : mimeOf(name, segment.type) }\n}\n\n/** The media segment types adapters send as attachments. */\nexport const MEDIA_SEGMENTS = new Set(['image', 'record', 'video', 'file'])\n"],"mappings":";;;AAYA,MAAM,OAA+B;CACnC,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,QAAQ;CACR,QAAQ;AACV;AAEA,MAAM,cAAsC;CAAE,OAAO;CAAQ,QAAQ;CAAQ,OAAO;CAAQ,MAAM;AAAO;;AAGzG,SAAgB,OAAO,MAAc,OAAO,QAAQ;CAClD,OAAO,KAAK,QAAQ,IAAI,CAAC,CAAC,YAAY,OAAO,SAAS,UAAU,cAAc,SAAS,WAAW,eAAe,SAAS,UAAU,cAAc;AACpJ;;AAGA,eAAsB,YAAY,SAAwC;CACxE,MAAM,QAAQ,OAAO,QAAQ,KAAK,OAAO,QAAQ,KAAK,QAAQ,QAAQ,KAAK,QAAQ,EAAE;CACrF,MAAM,QAAQ,OAAO,QAAQ,KAAK,SAAS,YAAY,QAAQ,KAAK,OAAO,QAAQ,KAAK,OAAO,KAAA;CAC/F,IAAI,CAAC,OAAO,MAAM,IAAI,MAAM,OAAO,QAAQ,KAAK,4BAA4B;CAC5E,MAAM,eAAe,SAAS,GAAG,QAAQ,OAAO,YAAY,QAAQ,SAAS;CAC7E,IAAI,gBAAgB,KAAK,KAAK,GAAG,OAAO;EAAE,MAAM;EAAO,KAAK;EAAO,GAAI,QAAQ,EAAE,MAAM,MAAM,IAAI,CAAC;CAAG;CACrG,IAAI,MAAM,WAAW,WAAW,GAC9B,OAAO;EAAE,MAAM;EAAS,OAAO,OAAO,KAAK,MAAM,MAAM,CAAC,GAAG,QAAQ;EAAG,MAAM;EAAc,MAAM,OAAO,cAAc,QAAQ,IAAI;CAAE;CAErI,MAAM,OAAO,mCAAmC,KAAK,KAAK;CAC1D,IAAI,MAEF,OAAO;EAAE,MAAM;EAAS,OADV,KAAK,KAAK,OAAO,KAAK,KAAK,IAAI,QAAQ,IAAI,OAAO,KAAK,mBAAmB,KAAK,EAAE,CAAC;EACjE,MAAM;EAAc,MAAM,KAAK,MAAM,OAAO,cAAc,QAAQ,IAAI;CAAE;CAEzG,MAAM,OAAO,MAAM,QAAQ,kBAAkB,QAAQ,aAAa,UAAU,KAAK,GAAG;CACpF,MAAM,OAAO,SAAS,SAAS,IAAI;CACnC,OAAO;EAAE,MAAM;EAAS,OAAO,MAAM,SAAS,IAAI;EAAG;EAAM,MAAM,OAAO,MAAM,QAAQ,IAAI;CAAE;AAC9F;;AAGA,eAAsB,WAAW,SAAkB,UAAU,KAAQ;CACnE,MAAM,SAAS,MAAM,YAAY,OAAO;CACxC,IAAI,OAAO,SAAS,SAAS,OAAO;CACpC,MAAM,WAAW,MAAM,MAAM,OAAO,KAAK,EAAE,QAAQ,YAAY,QAAQ,OAAO,EAAE,CAAC;CACjF,IAAI,CAAC,SAAS,IAAI,MAAM,IAAI,MAAM,eAAe,OAAO,IAAI,gBAAgB,SAAS,QAAQ;CAC7F,MAAM,QAAQ,OAAO,KAAK,MAAM,SAAS,YAAY,CAAC;CACtD,MAAM,OAAO,OAAO,SAAS,SAAS,IAAI,IAAI,OAAO,GAAG,CAAC,CAAC,QAAQ,KAAK,GAAG,QAAQ,OAAO,YAAY,QAAQ,SAAS;CACtH,MAAM,SAAS,SAAS,QAAQ,IAAI,cAAc,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC;CAChE,OAAO;EAAE,MAAM;EAAkB;EAAO;EAAM,MAAM,UAAU,WAAW,6BAA6B,SAAS,OAAO,MAAM,QAAQ,IAAI;CAAE;AAC5I;;AAGA,MAAa,iCAAiB,IAAI,IAAI;CAAC;CAAS;CAAU;CAAS;AAAM,CAAC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -15,6 +15,11 @@ export interface BotRecord {
|
|
|
15
15
|
config: Record<string, any>;
|
|
16
16
|
};
|
|
17
17
|
plugins: BotPluginEntry[];
|
|
18
|
+
/**
|
|
19
|
+
* App plugins (by package) whose chat commands this bot answers. A command an app plugin adds with
|
|
20
|
+
* `bots.defineCommand` is off on a bot until it is turned on here, unless the plugin picks the bots itself.
|
|
21
|
+
*/
|
|
22
|
+
appPlugins?: string[];
|
|
18
23
|
/** Default provider id for this bot's LLM plugins. */
|
|
19
24
|
provider?: string;
|
|
20
25
|
/** Command prefixes, the built-in `/help` and admins. */
|
|
@@ -51,6 +56,26 @@ export interface AdapterType<C = any> {
|
|
|
51
56
|
user?: string;
|
|
52
57
|
group?: string;
|
|
53
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* Config fields the dashboard leaves out, by the value of the config's `mode` field (its schema
|
|
61
|
+
* default when unset); `*` lists fields hidden in every mode, such as API addresses for tests.
|
|
62
|
+
*/
|
|
63
|
+
hiddenFields?: Record<string, string[]>;
|
|
64
|
+
/** Pages that help set a bot up, such as the platform's developer portal, shown as buttons by the form. */
|
|
65
|
+
links?: {
|
|
66
|
+
url: string;
|
|
67
|
+
label: LocalizedText;
|
|
68
|
+
}[];
|
|
69
|
+
/**
|
|
70
|
+
* Creating or binding a bot by scanning a QR code, through two actions the adapter defines:
|
|
71
|
+
* `start` answers `{ id, qrcode, interval }` (seconds between polls), where `qrcode` is the text
|
|
72
|
+
* to draw as a QR code; `poll` takes `{ id }` and answers `{ status }`, one of `pending`, `created`
|
|
73
|
+
* (with the adapter `config` to fill in), `expired` or `error` (with a `message`).
|
|
74
|
+
*/
|
|
75
|
+
qrSetup?: {
|
|
76
|
+
start: string;
|
|
77
|
+
poll: string;
|
|
78
|
+
};
|
|
54
79
|
/**
|
|
55
80
|
* Loaded inside each bot's isolated context with the bot's adapter config.
|
|
56
81
|
* It injects `bot`, opens the connection and registers a transport.
|
|
@@ -158,4 +183,32 @@ export interface BotEvent {
|
|
|
158
183
|
text: string;
|
|
159
184
|
segments: Segment[];
|
|
160
185
|
};
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* A message a plugin handled on a bot, for the bot's runs beside the flow runs: it replied, kept the
|
|
189
|
+
* message from the plugins after it, or failed. Plugins that only looked at the message leave no run.
|
|
190
|
+
*/
|
|
191
|
+
export interface PluginRun {
|
|
192
|
+
id: string;
|
|
193
|
+
botId: string;
|
|
194
|
+
/** The plugin's package, or its name when the package is not known. */
|
|
195
|
+
plugin: string;
|
|
196
|
+
/** The command the plugin answered, such as `天气`. */
|
|
197
|
+
command?: string;
|
|
198
|
+
/** The message, cut to 200 characters. */
|
|
199
|
+
text: string;
|
|
200
|
+
user: UserInfo;
|
|
201
|
+
/** `group:123` or `private:456`. */
|
|
202
|
+
channel: string;
|
|
203
|
+
/** The bot's record of the message (its `MessageRecord` id). */
|
|
204
|
+
message: string;
|
|
205
|
+
status: 'ok' | 'error';
|
|
206
|
+
/** Messages the plugin sent while handling it. */
|
|
207
|
+
replies: number;
|
|
208
|
+
/** The plugin did not pass the message on to the plugins after it. */
|
|
209
|
+
kept: boolean;
|
|
210
|
+
startedAt: number;
|
|
211
|
+
/** Time spent in the plugin itself, not in the plugins after it. */
|
|
212
|
+
ms: number;
|
|
213
|
+
error?: string;
|
|
161
214
|
}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { CommandSettings } from './commands'\nimport type { ChannelInfo, LocalizedText, Plugin, Provenance, Segment, UserInfo, z } from '@mushinjs/core'\n\nexport type BotStatus = 'offline' | 'connecting' | 'online' | 'error'\n\nexport interface BotPluginEntry {\n package: string\n enabled: boolean\n config: Record<string, any>\n}\n\nexport interface BotRecord {\n id: string\n name: string\n enabled: boolean\n adapter: { type: string; config: Record<string, any> }\n plugins: BotPluginEntry[]\n /** Default provider id for this bot's LLM plugins. */\n provider?: string\n /** Command prefixes, the built-in `/help` and admins. */\n commands?: CommandSettings\n createdAt: number\n}\n\n/** How an adapter delivers outgoing messages and raw API calls for one bot. */\nexport interface Transport {\n send(channel: ChannelInfo, segments: Segment[]): Promise<{ messageId?: string }>\n call?(action: string, params: Record<string, any>): Promise<any>\n}\n\nexport interface AdapterType<C = any> {\n id: string\n displayName: LocalizedText\n config: z.ZodType<C>\n /** Config keys holding secrets. They are kept in .env (the config holds `${NAME}` references), resolved before the adapter starts, and masked in API responses. */\n secrets?: string[]\n /** Segment types this adapter can send; used to compute a message's supported platforms. */\n segments: string[]\n /** The adapter can tell a sender's group role (owner, admin, member); its bots call `bot.useRoleResolver()`. */\n groupRoles?: boolean\n /**\n * How a bot with this config connects, for the bots list: a mode such as `ws`, `ws-reverse` or\n * `http`, the address it dials or is reached at, and for HTTP the path events are posted to.\n */\n describe?(config: C, botId: string): BotConnectionInfo\n /**\n * Addresses of people's and groups' pictures, with `{id}` standing for the user or group id,\n * for the dashboard's live conversation view. Left out when the platform has no such addresses.\n */\n avatars?: { user?: string; group?: string }\n /**\n * Loaded inside each bot's isolated context with the bot's adapter config.\n * It injects `bot`, opens the connection and registers a transport.\n */\n plugin: Plugin\n}\n\nexport interface BotConnectionInfo {\n mode: string\n address?: string\n inbound?: string\n}\n\nexport interface IncomingMessage {\n messageId?: string\n channel: ChannelInfo\n user: UserInfo\n segments: Segment[]\n timestamp?: number\n}\n\n/**\n * A sender's role on one bot, from the permissions plugin: `master` owns the bot, `admin` helps\n * manage it, `user` is everyone else. Higher roles include lower ones.\n */\nexport type BotRole = 'master' | 'admin' | 'user'\n\n/** A sender's role in a group, as the platform reports it. Kept apart from bot roles. */\nexport type GroupRole = 'owner' | 'admin' | 'member'\n\nexport const ROLE_RANK: Record<BotRole, number> = { user: 0, admin: 1, master: 2 }\n\n/** Whether `role` meets `required`: a master passes where an admin is required. */\nexport function roleAtLeast(role: BotRole | undefined, required: BotRole) {\n return role !== undefined && ROLE_RANK[role] >= ROLE_RANK[required]\n}\n\n/** Who someone is to a bot; supplied by the permissions plugin through `bots.usePermissions()`. */\nexport interface PermissionProvider {\n roleOf(botId: string, userId: string): BotRole\n}\n\n/** Asks the platform for a sender's group role; registered per bot by adapters that can tell. */\nexport type GroupRoleResolver = (target: { channel: ChannelInfo; user: UserInfo }) => Promise<GroupRole | undefined>\n\n/** A file a message points at, as an address the dashboard can open. */\nexport interface ResolvedMedia {\n /** An http(s) address, or a `data:` URL when the bytes were fetched for showing in place. */\n url: string\n name?: string\n size?: number\n}\n\n/** One message of a forwarded chat history. */\nexport interface ForwardedMessage {\n user: UserInfo\n timestamp: number\n segments: Segment[]\n}\n\n/**\n * Fetches what a message only points at: images, videos and files (whose addresses in the message\n * can expire) and the messages of a forwarded chat history. Registered per bot by adapters.\n */\nexport interface MediaResolver {\n /** `inline`: return the bytes as a data URL, for showing an image in place. */\n media?(segment: Segment, options: { channel?: ChannelInfo; inline: boolean }): Promise<ResolvedMedia | undefined>\n forward?(id: string): Promise<ForwardedMessage[]>\n}\n\n/** A friend or group the bot's account has, from the platform. */\nexport interface Contact extends ChannelInfo {\n /** The name the account gave a friend, when it differs from the friend's own. */\n remark?: string\n /** How many members a group has. */\n members?: number\n}\n\n/** Lists the account's friends and groups; registered per bot by adapters that can. */\nexport type ContactResolver = () => Promise<Contact[]>\n\n/**\n * The sandbox conversation every bot has in the live view: the dashboard plays a test user in a\n * private chat, the bot answers through its flows and plugins, and nothing reaches the platform.\n */\nexport const SANDBOX_CHANNEL: ChannelInfo = { type: 'private', id: '@sandbox', name: 'Sandbox' }\nexport const SANDBOX_USER = { id: '@sandbox', name: 'Sandbox tester' }\n\nexport const isSandbox = (channel: { id: string } | undefined) => channel?.id === SANDBOX_CHANNEL.id\n\n/** Content plus an explicit source, e.g. an LLM result (`{ text, source }`). */\nexport interface SourcedContent {\n text?: string\n segments?: Segment[]\n source?: Provenance\n}\n\n/** A non-message event from an adapter, such as a notice or a request. */\nexport interface BotEvent {\n /** The adapter's event family, e.g. OneBot's `notice` or `request`. */\n type: string\n /** The event as the platform sent it. */\n data: any\n /** A normalized name such as `group_increase` or `notify.poke`; flows only see events that have one. */\n name?: string\n /** Where the event happened, so flows can reply there. */\n channel?: ChannelInfo\n /** Who the event is about. */\n user?: UserInfo\n /** The message the event is about, when it is known (e.g. the text of a recalled message). */\n message?: { id?: string; text: string; segments: Segment[] }\n}\n"],"mappings":";
|
|
1
|
+
{"version":3,"file":"types.js","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type { CommandSettings } from './commands'\nimport type { ChannelInfo, LocalizedText, Plugin, Provenance, Segment, UserInfo, z } from '@mushinjs/core'\n\nexport type BotStatus = 'offline' | 'connecting' | 'online' | 'error'\n\nexport interface BotPluginEntry {\n package: string\n enabled: boolean\n config: Record<string, any>\n}\n\nexport interface BotRecord {\n id: string\n name: string\n enabled: boolean\n adapter: { type: string; config: Record<string, any> }\n plugins: BotPluginEntry[]\n /**\n * App plugins (by package) whose chat commands this bot answers. A command an app plugin adds with\n * `bots.defineCommand` is off on a bot until it is turned on here, unless the plugin picks the bots itself.\n */\n appPlugins?: string[]\n /** Default provider id for this bot's LLM plugins. */\n provider?: string\n /** Command prefixes, the built-in `/help` and admins. */\n commands?: CommandSettings\n createdAt: number\n}\n\n/** How an adapter delivers outgoing messages and raw API calls for one bot. */\nexport interface Transport {\n send(channel: ChannelInfo, segments: Segment[]): Promise<{ messageId?: string }>\n call?(action: string, params: Record<string, any>): Promise<any>\n}\n\nexport interface AdapterType<C = any> {\n id: string\n displayName: LocalizedText\n config: z.ZodType<C>\n /** Config keys holding secrets. They are kept in .env (the config holds `${NAME}` references), resolved before the adapter starts, and masked in API responses. */\n secrets?: string[]\n /** Segment types this adapter can send; used to compute a message's supported platforms. */\n segments: string[]\n /** The adapter can tell a sender's group role (owner, admin, member); its bots call `bot.useRoleResolver()`. */\n groupRoles?: boolean\n /**\n * How a bot with this config connects, for the bots list: a mode such as `ws`, `ws-reverse` or\n * `http`, the address it dials or is reached at, and for HTTP the path events are posted to.\n */\n describe?(config: C, botId: string): BotConnectionInfo\n /**\n * Addresses of people's and groups' pictures, with `{id}` standing for the user or group id,\n * for the dashboard's live conversation view. Left out when the platform has no such addresses.\n */\n avatars?: { user?: string; group?: string }\n /**\n * Config fields the dashboard leaves out, by the value of the config's `mode` field (its schema\n * default when unset); `*` lists fields hidden in every mode, such as API addresses for tests.\n */\n hiddenFields?: Record<string, string[]>\n /** Pages that help set a bot up, such as the platform's developer portal, shown as buttons by the form. */\n links?: { url: string; label: LocalizedText }[]\n /**\n * Creating or binding a bot by scanning a QR code, through two actions the adapter defines:\n * `start` answers `{ id, qrcode, interval }` (seconds between polls), where `qrcode` is the text\n * to draw as a QR code; `poll` takes `{ id }` and answers `{ status }`, one of `pending`, `created`\n * (with the adapter `config` to fill in), `expired` or `error` (with a `message`).\n */\n qrSetup?: { start: string; poll: string }\n /**\n * Loaded inside each bot's isolated context with the bot's adapter config.\n * It injects `bot`, opens the connection and registers a transport.\n */\n plugin: Plugin\n}\n\nexport interface BotConnectionInfo {\n mode: string\n address?: string\n inbound?: string\n}\n\nexport interface IncomingMessage {\n messageId?: string\n channel: ChannelInfo\n user: UserInfo\n segments: Segment[]\n timestamp?: number\n}\n\n/**\n * A sender's role on one bot, from the permissions plugin: `master` owns the bot, `admin` helps\n * manage it, `user` is everyone else. Higher roles include lower ones.\n */\nexport type BotRole = 'master' | 'admin' | 'user'\n\n/** A sender's role in a group, as the platform reports it. Kept apart from bot roles. */\nexport type GroupRole = 'owner' | 'admin' | 'member'\n\nexport const ROLE_RANK: Record<BotRole, number> = { user: 0, admin: 1, master: 2 }\n\n/** Whether `role` meets `required`: a master passes where an admin is required. */\nexport function roleAtLeast(role: BotRole | undefined, required: BotRole) {\n return role !== undefined && ROLE_RANK[role] >= ROLE_RANK[required]\n}\n\n/** Who someone is to a bot; supplied by the permissions plugin through `bots.usePermissions()`. */\nexport interface PermissionProvider {\n roleOf(botId: string, userId: string): BotRole\n}\n\n/** Asks the platform for a sender's group role; registered per bot by adapters that can tell. */\nexport type GroupRoleResolver = (target: { channel: ChannelInfo; user: UserInfo }) => Promise<GroupRole | undefined>\n\n/** A file a message points at, as an address the dashboard can open. */\nexport interface ResolvedMedia {\n /** An http(s) address, or a `data:` URL when the bytes were fetched for showing in place. */\n url: string\n name?: string\n size?: number\n}\n\n/** One message of a forwarded chat history. */\nexport interface ForwardedMessage {\n user: UserInfo\n timestamp: number\n segments: Segment[]\n}\n\n/**\n * Fetches what a message only points at: images, videos and files (whose addresses in the message\n * can expire) and the messages of a forwarded chat history. Registered per bot by adapters.\n */\nexport interface MediaResolver {\n /** `inline`: return the bytes as a data URL, for showing an image in place. */\n media?(segment: Segment, options: { channel?: ChannelInfo; inline: boolean }): Promise<ResolvedMedia | undefined>\n forward?(id: string): Promise<ForwardedMessage[]>\n}\n\n/** A friend or group the bot's account has, from the platform. */\nexport interface Contact extends ChannelInfo {\n /** The name the account gave a friend, when it differs from the friend's own. */\n remark?: string\n /** How many members a group has. */\n members?: number\n}\n\n/** Lists the account's friends and groups; registered per bot by adapters that can. */\nexport type ContactResolver = () => Promise<Contact[]>\n\n/**\n * The sandbox conversation every bot has in the live view: the dashboard plays a test user in a\n * private chat, the bot answers through its flows and plugins, and nothing reaches the platform.\n */\nexport const SANDBOX_CHANNEL: ChannelInfo = { type: 'private', id: '@sandbox', name: 'Sandbox' }\nexport const SANDBOX_USER = { id: '@sandbox', name: 'Sandbox tester' }\n\nexport const isSandbox = (channel: { id: string } | undefined) => channel?.id === SANDBOX_CHANNEL.id\n\n/** Content plus an explicit source, e.g. an LLM result (`{ text, source }`). */\nexport interface SourcedContent {\n text?: string\n segments?: Segment[]\n source?: Provenance\n}\n\n/** A non-message event from an adapter, such as a notice or a request. */\nexport interface BotEvent {\n /** The adapter's event family, e.g. OneBot's `notice` or `request`. */\n type: string\n /** The event as the platform sent it. */\n data: any\n /** A normalized name such as `group_increase` or `notify.poke`; flows only see events that have one. */\n name?: string\n /** Where the event happened, so flows can reply there. */\n channel?: ChannelInfo\n /** Who the event is about. */\n user?: UserInfo\n /** The message the event is about, when it is known (e.g. the text of a recalled message). */\n message?: { id?: string; text: string; segments: Segment[] }\n}\n\n/**\n * A message a plugin handled on a bot, for the bot's runs beside the flow runs: it replied, kept the\n * message from the plugins after it, or failed. Plugins that only looked at the message leave no run.\n */\nexport interface PluginRun {\n id: string\n botId: string\n /** The plugin's package, or its name when the package is not known. */\n plugin: string\n /** The command the plugin answered, such as `天气`. */\n command?: string\n /** The message, cut to 200 characters. */\n text: string\n user: UserInfo\n /** `group:123` or `private:456`. */\n channel: string\n /** The bot's record of the message (its `MessageRecord` id). */\n message: string\n status: 'ok' | 'error'\n /** Messages the plugin sent while handling it. */\n replies: number\n /** The plugin did not pass the message on to the plugins after it. */\n kept: boolean\n startedAt: number\n /** Time spent in the plugin itself, not in the plugins after it. */\n ms: number\n error?: string\n}\n"],"mappings":";AAmGA,MAAa,YAAqC;CAAE,MAAM;CAAG,OAAO;CAAG,QAAQ;AAAE;;AAGjF,SAAgB,YAAY,MAA2B,UAAmB;CACxE,OAAO,SAAS,KAAA,KAAa,UAAU,SAAS,UAAU;AAC5D;;;;;AAkDA,MAAa,kBAA+B;CAAE,MAAM;CAAW,IAAI;CAAY,MAAM;AAAU;AAC/F,MAAa,eAAe;CAAE,IAAI;CAAY,MAAM;AAAiB;AAErE,MAAa,aAAa,YAAwC,SAAS,OAAO,gBAAgB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mushinjs/plugin-bots",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-next.20261124175848",
|
|
4
4
|
"description": "Bot manager and per-bot runtimes",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,9 +52,9 @@
|
|
|
52
52
|
}
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@mushinjs/core": "0.
|
|
56
|
-
"@mushinjs/plugin-database": "0.
|
|
57
|
-
"@mushinjs/plugin-storage": "0.
|
|
55
|
+
"@mushinjs/core": "0.4.0-next.20261124175848",
|
|
56
|
+
"@mushinjs/plugin-database": "0.4.0-next.20261124175848",
|
|
57
|
+
"@mushinjs/plugin-storage": "0.4.0-next.20261124175848",
|
|
58
58
|
"minato": "4.0.1"
|
|
59
59
|
}
|
|
60
60
|
}
|