feat(onebot): add optional returnsSchema to action doc metadata
This commit is contained in:
2 files changed
+56
No files matched your search
@@ -331,6 +331,10 @@ export interface ActionDoc {
|
|||||||
category?: string;
|
category?: string;
|
||||||
summary?: string;
|
summary?: string;
|
||||||
returns?: string;
|
returns?: string;
|
||||||
|
/** JSON Schema for the `data` payload inside the OneBot envelope (NOT the
|
||||||
|
* whole envelope). Optional + authored incrementally; absent ⇒ data shape
|
||||||
|
* is documented only by the prose `returns`. */
|
||||||
|
returnsSchema?: JsonSchema;
|
||||||
/** True only for pure data-fetch actions (no side effects). Drives the MCP's
|
/** True only for pure data-fetch actions (no side effects). Drives the MCP's
|
||||||
* read/write tool routing; defaults to false (treated as a write). */
|
* read/write tool routing; defaults to false (treated as a write). */
|
||||||
readOnly: boolean;
|
readOnly: boolean;
|
||||||
@@ -358,6 +362,8 @@ interface ActionDef<S extends Spec> {
|
|||||||
name: string | readonly [string, ...string[]];
|
name: string | readonly [string, ...string[]];
|
||||||
summary?: string;
|
summary?: string;
|
||||||
returns?: string;
|
returns?: string;
|
||||||
|
/** JSON Schema for the `data` payload (NOT the envelope). See ActionDoc. */
|
||||||
|
returnsSchema?: JsonSchema;
|
||||||
/** Mark true ONLY for pure data-fetch actions with no side effects. Default
|
/** Mark true ONLY for pure data-fetch actions with no side effects. Default
|
||||||
* false = write. Surfaced via describe() into the catalog for the MCP's
|
* false = write. Surfaced via describe() into the catalog for the MCP's
|
||||||
* read/write routing. Classify by what `run` actually does, not the name. */
|
* read/write routing. Classify by what `run` actually does, not the name. */
|
||||||
@@ -425,6 +431,7 @@ export function defineAction<S extends Spec>(def: ActionDef<S>): ActionSpec<S> {
|
|||||||
aliases: names.slice(1),
|
aliases: names.slice(1),
|
||||||
summary: def.summary,
|
summary: def.summary,
|
||||||
returns: def.returns,
|
returns: def.returns,
|
||||||
|
returnsSchema: def.returnsSchema,
|
||||||
readOnly: def.readOnly ?? false,
|
readOnly: def.readOnly ?? false,
|
||||||
params: entries.map(([name, field]) => ({ name, ...field.doc })),
|
params: entries.map(([name, field]) => ({ name, ...field.doc })),
|
||||||
invariants: rules.map((rule) => rule.doc),
|
invariants: rules.map((rule) => rule.doc),
|
||||||
@@ -450,6 +457,7 @@ interface PresetDef<S extends Spec, Extra> {
|
|||||||
name: string | readonly [string, ...string[]];
|
name: string | readonly [string, ...string[]];
|
||||||
summary?: string;
|
summary?: string;
|
||||||
returns?: string;
|
returns?: string;
|
||||||
|
returnsSchema?: JsonSchema;
|
||||||
readOnly?: boolean;
|
readOnly?: boolean;
|
||||||
params?: S;
|
params?: S;
|
||||||
rules?: (r: RuleBuilders<InferParams<S> & Extra>) => readonly CrossFieldRule[];
|
rules?: (r: RuleBuilders<InferParams<S> & Extra>) => readonly CrossFieldRule[];
|
||||||
|
|||||||
@@ -0,0 +1,48 @@
|
|||||||
|
// Tests the optional returnsSchema doc metadata: it must round-trip through
|
||||||
|
// describe() (on both defineAction and the group* presets) and stay absent
|
||||||
|
// when not provided. Semantics: returnsSchema describes the `data` payload
|
||||||
|
// inside the OneBot envelope, not the envelope.
|
||||||
|
import { describe, it, expect } from 'vitest';
|
||||||
|
import { defineAction, groupAction, f } from '../src/action-kit';
|
||||||
|
import { okResponse } from '../src/types';
|
||||||
|
|
||||||
|
describe('returnsSchema', () => {
|
||||||
|
const dataSchema = {
|
||||||
|
type: 'object',
|
||||||
|
properties: { group_id: { type: 'integer' }, group_name: { type: 'string' } },
|
||||||
|
required: ['group_id', 'group_name'],
|
||||||
|
};
|
||||||
|
|
||||||
|
it('round-trips returnsSchema through describe() on defineAction', () => {
|
||||||
|
const spec = defineAction({
|
||||||
|
name: 'demo_get',
|
||||||
|
summary: 'demo',
|
||||||
|
readOnly: true,
|
||||||
|
returns: '群信息对象',
|
||||||
|
returnsSchema: dataSchema,
|
||||||
|
params: { x: f.uint() },
|
||||||
|
run: () => okResponse({ ok: true }),
|
||||||
|
});
|
||||||
|
expect(spec.describe().returnsSchema).toEqual(dataSchema);
|
||||||
|
expect(spec.describe().returns).toBe('群信息对象');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('omits returnsSchema when not provided', () => {
|
||||||
|
const spec = defineAction({
|
||||||
|
name: 'demo_noschema',
|
||||||
|
params: {},
|
||||||
|
run: () => okResponse(null),
|
||||||
|
});
|
||||||
|
expect(spec.describe().returnsSchema).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('carries returnsSchema through groupAction preset', () => {
|
||||||
|
const spec = groupAction({
|
||||||
|
name: 'demo_group_get',
|
||||||
|
readOnly: true,
|
||||||
|
returnsSchema: dataSchema,
|
||||||
|
run: () => okResponse({}),
|
||||||
|
});
|
||||||
|
expect(spec.describe().returnsSchema).toEqual(dataSchema);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user