Skip to content

Commit de7d8a9

Browse files
authored
fix(minimax-bot): 声明了的值就照声明校验,名字写在目录外的头像不再被读进来 (#95)
1 parent 1aa1679 commit de7d8a9

11 files changed

Lines changed: 983 additions & 30 deletions

File tree

‎plugins/minimax-ai/minimax-bot/.minimax-plugin/plugin.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"schemaVersion": 1,
33
"name": "minimax-bot",
44
"displayName": "MiniMax Bot",
5-
"version": "0.7.14",
5+
"version": "0.7.15",
66
"description": "Turn MiniMax Code into an always-on team of Bots. A Chief Bot helps you build the team; in group chats the admin answers and @mentions hand work to other members. Every Bot has its own octopus avatar and memory, with scheduled tasks and usage stats.",
77
"author": "MiniMax Code",
88
"icon": "icon.png",

‎plugins/minimax-ai/minimax-bot/README.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ Text that travels from one Bot into another Bot's prompt is cleaned on the way.
3636

3737
Only the tag goes — the words around it stay — and the plugin's own delivery format (`<media src="…" />`) is not touched, so a report that names a file still reaches the Bot that has to hand it over.
3838

39+
## 0.7.15 What the plugin refuses to guess
40+
41+
A value the plugin declares is now checked at the door instead of being quietly turned into another legal value. A scheduled task whose `kind` is not `daily`, `interval` or `once` is refused with the list of the values it may take — leaving `kind` out is still the default daily task — and a group whose `reply_mode` is not `admin` or `all` is refused too, without touching the setting the group already had. The usage range has to be a whole number of days from 1 to 90; a value outside it gets an error instead of an empty chart that reads like the quota had been wiped.
42+
43+
The MCP tools check the arguments they declare before they run. A value of the wrong JSON type is refused at the door, so a call that does not fit the schema never reaches the tool and never changes anything. For the enumerations a tool answers itself — the memory scope, the group reply mode, the schedule kind — the tool's own message is the one the model reads: it names the range and what the value would have changed, which a schema cannot. A failure that is not one of the plugin's own errors answers with a plain notice now, and the detail goes to the log instead of into the model's context.
44+
45+
An avatar may come from the avatar library or from an image uploaded in the page only. A path on the computer is refused, the plugin reads no file outside its own `dataDir` for one, and the refused path is not echoed back — so the field cannot be used to ask what exists on the disk.
46+
3947
## 0.7.10 Optional group notes
4048

4149
- A group can keep a note of what it said. After every round the group records the closing lines of the replies, and every Bot of the group gets that record in its standing brief.

‎plugins/minimax-ai/minimax-bot/miniapp/node/mcp.mjs‎

Lines changed: 85 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { EngineError } from './engine.mjs';
33
import { t } from './i18n.mjs';
44
import { ENTRY_MAX, MemoryError, TOPIC_KEY_RE } from './memory.mjs';
55
import { BOT_COLORS, BOT_SKINS, effortOptions, withEffort, newId } from './store.mjs';
6+
import { MEMORY_SCOPES } from './team.mjs';
67

78
/** @typedef {import('./team.mjs').Team} Team */
89
/** @typedef {import('./engine.mjs').Engine} Engine */
@@ -36,7 +37,7 @@ function toolDefs() {
3637
};
3738
const memoryScope = {
3839
type: 'string',
39-
enum: ['self', 'team', 'bot'],
40+
enum: MEMORY_SCOPES,
4041
description: t('mcp.p.scope'),
4142
};
4243

@@ -271,6 +272,67 @@ function toolDefs() {
271272

272273
/** Tool names, for lookups that must not depend on the language. */
273274
export const TOOL_NAMES = toolDefs().map((tool) => tool.name);
275+
276+
/** Whether a value is of the JSON type a schema declares. */
277+
function isJsonType(type, value) {
278+
if (type === 'string') return typeof value === 'string';
279+
if (type === 'boolean') return typeof value === 'boolean';
280+
if (type === 'integer') return Number.isInteger(value);
281+
if (type === 'number') return typeof value === 'number' && Number.isFinite(value);
282+
if (type === 'array') return Array.isArray(value);
283+
if (type === 'object') return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
284+
return true;
285+
}
286+
287+
/**
288+
* The enums a tool answers itself.
289+
*
290+
* For these the tool's answer says more than the schema can: the range, and what the value
291+
* would have changed had it been accepted. So the value reaches the tool and the model reads
292+
* the tool's message, not a generic one. Every other declared enum is checked here, before the
293+
* tool runs, so a value that would otherwise be read as a different legal one never changes
294+
* anything.
295+
*/
296+
const TOOL_OWNED_ENUMS = new Set([
297+
'remember.scope', 'forget.scope', 'recall.scope',
298+
'create_group.reply_mode', 'update_group.reply_mode',
299+
'create_schedule.kind',
300+
]);
301+
302+
/**
303+
* What is wrong with the arguments of a tool call, judged by the inputSchema the tool itself
304+
* declares: a value of the wrong JSON type, or a value outside a declared enum. The other rules
305+
* of a tool (a topic a memory needs, a text that may not be longer than X) belong to the tool,
306+
* which answers them with its own message; here only the shape of a value is checked, so a
307+
* call that does not fit the schema never reaches the tool and never changes anything.
308+
* @param {string} tool the tool being called, for the enums the tool answers itself
309+
* @param {any} schema
310+
* @param {any} args
311+
* @returns {string[]} the problems, empty when the arguments fit the schema
312+
*/
313+
function schemaProblems(tool, schema, args) {
314+
if (!schema || schema.type !== 'object') return [];
315+
if (args === null || typeof args !== 'object' || Array.isArray(args)) {
316+
return [t('mcp.err.argObject')];
317+
}
318+
const problems = [];
319+
for (const [name, value] of Object.entries(args)) {
320+
// An argument the tool did not declare is left to the tool: dispatch_tasks, for one, has a
321+
// message of its own for the options it does not support.
322+
const property = schema.properties?.[name];
323+
if (!property || value === null || value === undefined) continue;
324+
if (property.type && !isJsonType(property.type, value)) {
325+
problems.push(t('mcp.err.argType', { name, type: property.type }));
326+
continue;
327+
}
328+
if (Array.isArray(property.enum) && !property.enum.includes(value)
329+
&& !TOOL_OWNED_ENUMS.has(`${tool}.${name}`)) {
330+
problems.push(t('mcp.err.argEnum', { name, values: property.enum.join(', '), value: String(value) }));
331+
}
332+
}
333+
return problems;
334+
}
335+
274336
/**
275337
* @param {{ team: Team, engine: Engine }} deps
276338
*/
@@ -296,6 +358,20 @@ export function createMcpHandler({ team, engine }) {
296358
return bot;
297359
}
298360

361+
/**
362+
* The text a failed tool call shows the model. A failure the plugin knows (EngineError,
363+
* MemoryError) carries its own message, written for the model. Anything else is a fault of
364+
* the plugin: the model gets a generic line it cannot act on anyway, and the detail goes
365+
* to the log, where the operator reads it.
366+
*/
367+
function failureText(error, name) {
368+
if (error instanceof EngineError || error instanceof MemoryError) return String(error.message);
369+
engine.logger?.warn?.('minimax_bot.tool_failed', {
370+
tool: name, code: error?.code ?? 'unknown', detail: String(error?.message ?? error),
371+
});
372+
return t('mcp.err.internal');
373+
}
374+
299375
const LIVE = {
300376
// Sent as keys so the page renders them in its own language.
301377
message_bot: (a) => ({ key: 'live.askBot', params: { name: store.getBot(a.to)?.name ?? a.to ?? t('mcp.anotherBot') } }),
@@ -586,7 +662,7 @@ export function createMcpHandler({ team, engine }) {
586662
result = {
587663
protocolVersion: PROTOCOL_VERSIONS.includes(requested) ? requested : PROTOCOL_VERSIONS[0],
588664
capabilities: { tools: { listChanged: false } },
589-
serverInfo: { name: 'minimax-bot', version: '0.7.14' },
665+
serverInfo: { name: 'minimax-bot', version: '0.7.15' },
590666
instructions: t('mcp.instructions'),
591667
};
592668
break;
@@ -606,6 +682,12 @@ export function createMcpHandler({ team, engine }) {
606682
if (!TOOL_NAMES.includes(name)) {
607683
return { jsonrpc: '2.0', id, error: { code: -32602, message: `Unknown tool: ${name}` } };
608684
}
685+
const tool = toolDefs().find((entry) => entry.name === name);
686+
const problems = schemaProblems(name, tool?.inputSchema, args);
687+
if (problems.length) {
688+
result = { content: [{ type: 'text', text: t('mcp.err.badArgs', { tool: name, problems }) }], isError: true };
689+
break;
690+
}
609691
const callKey = session ? `${session}:${JSON.stringify(id)}` : newId('rpc');
610692
if (calls.has(callKey)) return { jsonrpc: '2.0', id, error: { code: -32600, message: 'Request already running' } };
611693
const controller = new AbortController();
@@ -614,8 +696,7 @@ export function createMcpHandler({ team, engine }) {
614696
const value = await callTool(name, args, controller.signal);
615697
result = { content: [{ type: 'text', text: JSON.stringify(value, null, name === 'recall' ? 0 : 2) }] };
616698
} catch (error) {
617-
const text = error && typeof error === 'object' && 'message' in error ? String(error.message) : String(error);
618-
result = { content: [{ type: 'text', text }], isError: true };
699+
result = { content: [{ type: 'text', text: failureText(error, name) }], isError: true };
619700
} finally { calls.delete(callKey); }
620701
break;
621702
}

‎plugins/minimax-ai/minimax-bot/miniapp/node/server.mjs‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ import { markdownDocument, HTML_PREVIEW_CSP, MARKDOWN_PREVIEW_CSP } from './docu
2222

2323
export const USER_NAME_MAX = 24;
2424

25+
/** The usage panel's range: a whole number of days, from one day to three months. */
26+
const USAGE_DAYS_DEFAULT = 7;
27+
const USAGE_DAYS_MIN = 1;
28+
const USAGE_DAYS_MAX = 90;
29+
2530
/**
2631
* The account's display name: the macOS full name (`id -F`), otherwise the login name. The Host
2732
* exposes no user identity to Mini Apps, so this is the best default; the user can override it.
@@ -178,9 +183,20 @@ export async function start(context) {
178183
return { scope, entries, chars: memoryBudget(entries).used, budget: memoryBudget(entries), contextBudget: { limit: MEMORY_CONTEXT_MAX, unit: 'characters' } };
179184
};
180185

181-
const usage = async (days) => {
182-
const hourly = days <= 1;
183-
const span = hourly ? 24 : Math.max(1, Math.min(90, days));
186+
/** The usage panel asks for a number of days; anything but a whole 1..90 is refused. */
187+
const usageDays = (raw) => {
188+
if (raw === undefined || raw === null) return USAGE_DAYS_DEFAULT;
189+
const days = Number(raw);
190+
if (!Number.isInteger(days) || days < USAGE_DAYS_MIN || days > USAGE_DAYS_MAX) {
191+
throw new EngineError('invalid', t('err.usageDays', { min: USAGE_DAYS_MIN, max: USAGE_DAYS_MAX }));
192+
}
193+
return days;
194+
};
195+
196+
const usage = async (raw) => {
197+
const days = usageDays(raw);
198+
const hourly = days === 1;
199+
const span = hourly ? 24 : days;
184200
const pad = (n) => String(n).padStart(2, '0');
185201
const start = new Date();
186202
if (hourly) {
@@ -658,7 +674,7 @@ export async function start(context) {
658674
}
659675

660676
if (method === 'GET' && path === '/api/usage') {
661-
return json(response, 200, await usage(Number(url.searchParams.get('days') ?? 7)));
677+
return json(response, 200, await usage(url.searchParams.get('days')));
662678
}
663679

664680
json(response, 404, { error: 'not_found', message: t('err.routeNotFound') });

0 commit comments

Comments
 (0)