Add payload and return schemas to OneBot actions (#1549)
* Add payload and return schemas to OneBot actions Introduced explicit payloadSchema and returnSchema definitions for all OneBotAction classes using @sinclair/typebox. This improves type safety, API documentation, and validation for action payloads and return values. Also refactored method signatures and types for consistency across the codebase. * Refactor payload schemas to use string IDs Replaced Type.Union([Type.Number(), Type.String()]) with Type.String for group_id, user_id, and similar fields across all action payload schemas to standardize input types. Also made minor improvements to error handling, return types, and removed unused imports for better code clarity and consistency. * Refactor type definitions and payload schemas in actions Standardized type usage and improved type safety across multiple OneBot action files. Updated payload schemas to use string types for IDs and flags, refined return types, and enhanced message content typing. Added error handling for missing parameters in SetGroupTodo. * Refactor type handling and improve message parsing Updated several actions to use more precise type casting and type guards, improving type safety and clarity. Enhanced message parsing logic for forward messages and group/friend message history. Standardized return schemas and error handling for avatar and group portrait actions. * Add napcat-schema package for OpenAPI generation Introduces the napcat-schema package with scripts and configuration to auto-generate OpenAPI schemas for NapCat OneBot 11 actions. Refactors action handler export in napcat-onebot to support schema extraction. * Add action examples and enhance action metadata Introduced a centralized examples.ts file providing payload and return examples for all actions. Updated numerous action classes to include actionDescription, actionTags, payloadExample, and returnExample fields, improving API documentation and discoverability. * Refactor action example imports and add example files Moved action example data to dedicated 'examples.ts' files for each action category (extends, file, go-cqhttp, group, msg, system, user). Updated all action classes to import and use the new example modules, improving code organization and maintainability. Also added missing actionTags and actionDescription where appropriate. * Update GetGroupMemberList.ts * Add actionSummary and improve action metadata Introduces the actionSummary property to OneBotAction and updates all action classes to provide concise summaries and improved descriptions. Refactors example imports for better modularity, adds new example files for guild and packet actions, and updates the OpenAPI schema generator to use the new summary and improved descriptions. This enhances API documentation clarity and consistency. * Enhance action metadata and add examples for new actions Added actionSummary, actionDescription, and actionTags to multiple OneBot actions for improved API documentation. Introduced payload and response examples for new actions (GetDoubtFriendsAddRequest, SetDoubtFriendsAddRequest) in a new examples.ts file. Also removed unused imports from several files for code clarity. * Refactor action examples and enhance metadata Replaced generic ActionExamples imports with more specific examples modules (FileActionsExamples, GroupActionsExamples, GoCQHTTPActionsExamples) across file, group, and go-cqhttp actions. Added or updated actionSummary, actionDescription, actionTags, payloadExample, and returnExample properties for improved API documentation and clarity. * Refactor extends actions to use new examples module Replaced imports of ActionExamples with ExtendsActionsExamples in all extends actions. Updated action summary, description, tags, and example references for consistency and clarity across actions. This improves maintainability and aligns with the new examples structure. * Add action metadata to OneBot action classes Added or updated actionSummary, actionTags, payloadExample, and returnExample properties for all OneBot action classes in the napcat-onebot package. This improves API documentation and discoverability by providing concise summaries, categorization tags, and usage examples for each action. * Refactor OpenAPI schema generation to 3.0.1 format Updated the OpenAPI schema output to use version 3.0.1, restructured tags, responses, and examples for better clarity and compatibility, and simplified output file locations. Also removed unused scripts from package.json. * Fix SendPokePayloadSchema type definitions Corrected the type definitions for user_id and target_id to only allow strings, and fixed a syntax error in group_id. This ensures payload validation is consistent and accurate. Refactor fileset ID API response and schema handling Updated GetFilesetId action to return a structured object with fileset_id and adjusted its return schema accordingly. Improved frontend TypeBox schema parsing to support allOf (intersection) merging and updated API debug component to construct response schemas in a more robust way for object recognition. Refactor OneBot API schema handling to use TypeBox Replaces Zod-based static API schema definitions with dynamic fetching of schemas from the backend using TypeBox. Removes legacy static schema files, updates frontend API debug components to use TypeBox utilities, and adds @sinclair/typebox as a dependency. Backend now exposes a /schemas endpoint for all OneBot actions. Various schema and description fields are updated for clarity and consistency. * Remove OneBot API navigation list component Deleted nav_list.tsx from the onebot/api components, removing the OneBotApiNavList React component and its related logic. This may be part of a refactor or cleanup to eliminate unused or redundant UI code. * Add action tags to OneBot API schema and update tag name Included the 'tags' property in the OneBot API schema for both backend and frontend, allowing actions to be categorized. Also updated the action tag from '群扩展' to '群组扩展' in SetGroupSign for consistency. * Add napcat-types package for unified type exports Introduced the napcat-types package to aggregate and re-export all types, enums, and classes from napcat-core and napcat-onebot. Added external module shims, test files, and configuration for type-only distribution. Updated core and onebot packages to improve export granularity and fixed import paths for better modularity. * Move external-shims.d.ts to files in tsconfig external-shims.d.ts was moved from the include array to the files array in tsconfig.json to ensure it is always included explicitly. This change clarifies the intent and may help with TypeScript's file resolution. Refactor napcat-types package and update plugin deps Refactored napcat-types to provide more accurate shims, added real type dependencies, and improved build/test scripts. Updated napcat-plugin and napcat-plugin-builtin to depend on napcat-types instead of napcat-onebot. Adjusted imports in affected packages to use napcat-types, and updated pnpm-lock.yaml accordingly. Add build and test scripts to napcat-types package Introduced 'build' and 'test' scripts in the napcat-types package.json for easier development and testing. Also updated dependencies in the lockfile. * 完善部分api描述 * Remove unused statusText constant Deleted the unused statusText constant from FetchCustomFace.ts to clean up the code. * Bump napcat-types version to 0.0.2 Updated the package version in package.public.json from 0.0.1 to 0.0.2. Update napcat-types package metadata and dependencies Set package as public by changing 'private' to false. Move 'napcat-core' and 'napcat-onebot' from dependencies to devDependencies, and remove 'compressing' from dependencies. Add public packaging and build script for napcat-types Introduces package.public.json and a copy-dist.mjs script to automate copying metadata and README into the dist folder for publishing. Updates build script in package.json to use the new copy step. Adds initial package.json and README for napcat-types. Update scripts in napcat-types package configs Added a publish script to package.json and removed scripts from package.public.json to streamline configuration and avoid duplication. * Update publish script to use npm in dist directory Changed the publish script to run 'npm publish' from the 'dist' directory instead of using 'pnpm publish --filter napcat-types'. This ensures the published package uses the built output. * Update pnpm-lock.yaml dependencies Removed 'compressing' from dependencies and cleaned up libc fields for various platform-specific packages. This streamlines the lock file and may improve cross-platform compatibility. * Add workflow to publish OpenAPI schema to NapCatDocs Introduces a new 'publish-schema' job in the auto-release workflow. This job builds the napcat-schema package, copies the generated OpenAPI schema to the NapCatDocs repository under a versioned path, and commits the update. Automates schema publishing on release events. * AI修正部分api文档 * Update OpenAPI version and use dynamic version from napcat-common Changed OpenAPI spec version to 3.1.0 and replaced the hardcoded API version with napCatVersion from napcat-common. Added napcat-common as a dependency in package.json. * Update napcat-schema build and OpenAPI version Renamed the build script from build:schema to build:openapi in napcat-schema and updated the workflow to use the new script. Changed OpenAPI version from 3.1.0 to 3.0.1 in the schema generator. Added napcat-vite as a dependency and integrated its version plugin into the Vite config. * 暂时OK * Refactor action examples structure and imports Moved action example files into a new 'example' directory and updated all imports accordingly. Removed the monolithic 'examples.ts' and redefined ActionExamples in OneBotAction.ts to only include common error codes. This improves code organization and maintainability. * Fix type for rate limiter middleware in router Casts the rate limiter middleware to RequestHandler to resolve type compatibility issues with Express router middleware. * Add OB11 message segment schemas and update SendMsg Introduces a comprehensive message segment schema (OB11) in a new file, refactors SendMsg payload to use the new OB11MessageMixTypeSchema, and updates related type definitions for improved type safety and extensibility in message handling. * Refactor OB11 message types to use TypeBox schemas Migrates all OB11 message segment and message type definitions from interface/enums to TypeBox schemas in types/message.ts. Removes the now-redundant message-segment-schema.ts file and updates imports to use the new schema-based types. This unifies type validation and TypeScript types, improving maintainability and consistency. --------- Co-authored-by: pohgxz <Ttjonne@outlook.com>
This commit is contained in:
1 parent
791e359199
commit
de9d5180fe
213 files changed
+6303
-4395
No files matched your search
@@ -1,56 +1,5 @@
|
||||
import { OB11Sender } from './data';
|
||||
import { EventType } from '@/napcat-onebot/event/OneBotEvent';
|
||||
import { CustomMusicSignPostData, IdMusicSignPostData, PicSubType, RawMessage } from 'napcat-core';
|
||||
import { Type, Static } from '@sinclair/typebox';
|
||||
|
||||
// 消息类型枚举
|
||||
export enum OB11MessageType {
|
||||
private = 'private',
|
||||
group = 'group',
|
||||
}
|
||||
|
||||
// 消息接口定义
|
||||
export interface OB11Message {
|
||||
real_seq?: string;// 自行扩展
|
||||
temp_source?: number;
|
||||
message_sent_type?: string;
|
||||
target_id?: number; // 自己发送消息/私聊消息
|
||||
self_id?: number;
|
||||
time: number;
|
||||
message_id: number;
|
||||
message_seq: number; // 和message_id一样
|
||||
real_id: number;
|
||||
user_id: number | string; // number
|
||||
group_id?: number | string; // number
|
||||
group_name?: string; // string
|
||||
message_type: 'private' | 'group';
|
||||
sub_type?: 'friend' | 'group' | 'normal';
|
||||
sender: OB11Sender;
|
||||
message: OB11MessageData[] | string;
|
||||
message_format: 'array' | 'string';
|
||||
raw_message: string;
|
||||
font: number;
|
||||
post_type?: EventType;
|
||||
raw?: RawMessage;
|
||||
emoji_likes_list?: Array<{ emoji_id: string; emoji_type: string; likes_cnt: string; }>;// 仅get_msg生效
|
||||
}
|
||||
|
||||
// 合并转发消息接口定义
|
||||
export interface OB11ForwardMessage extends OB11Message {
|
||||
content: OB11MessageData[] | string;
|
||||
}
|
||||
|
||||
// 返回数据接口定义
|
||||
export interface OB11Return<DataType> {
|
||||
status: string;
|
||||
retcode: number;
|
||||
data: DataType;
|
||||
message: string;
|
||||
echo?: unknown; // ws调用api才有此字段
|
||||
wording?: string; // go-cqhttp字段,错误信息
|
||||
stream?: 'stream-action' | 'normal-action'; // 流式返回标记
|
||||
}
|
||||
|
||||
// 消息数据类型枚举
|
||||
export enum OB11MessageDataType {
|
||||
text = 'text',
|
||||
image = 'image',
|
||||
@@ -76,126 +25,422 @@ export enum OB11MessageDataType {
|
||||
onlinefile = 'onlinefile', // 在线文件/文件夹
|
||||
flashtransfer = 'flashtransfer', // QQ闪传
|
||||
}
|
||||
// ==================== 基础消息段类型 ====================
|
||||
|
||||
export interface OB11MessagePoke {
|
||||
type: OB11MessageDataType.poke;
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
};
|
||||
}
|
||||
// 纯文本消息段
|
||||
export const OB11MessageTextSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.text),
|
||||
data: Type.Object({
|
||||
text: Type.String({ description: '纯文本内容' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageText', description: '纯文本消息段' });
|
||||
|
||||
// 商城表情消息接口定义
|
||||
export interface OB11MessageMFace {
|
||||
type: OB11MessageDataType.mface;
|
||||
data: {
|
||||
emoji_package_id: number;
|
||||
emoji_id: string;
|
||||
key: string;
|
||||
summary: string;
|
||||
};
|
||||
}
|
||||
// 表情消息段
|
||||
export const OB11MessageFaceSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.face),
|
||||
data: Type.Object({
|
||||
id: Type.String({ description: '表情ID' }),
|
||||
resultId: Type.Optional(Type.String({ description: '结果ID' })),
|
||||
chainCount: Type.Optional(Type.Number({ description: '连击数' })),
|
||||
}),
|
||||
}, { $id: 'OB11MessageFace', description: 'QQ表情消息段' });
|
||||
|
||||
// 纯文本消息接口定义
|
||||
export interface OB11MessageText {
|
||||
type: OB11MessageDataType.text;
|
||||
data: {
|
||||
text: string; // 纯文本
|
||||
};
|
||||
}
|
||||
// 商城表情消息段
|
||||
export const OB11MessageMFaceSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.mface),
|
||||
data: Type.Object({
|
||||
emoji_package_id: Type.Number({ description: '表情包ID' }),
|
||||
emoji_id: Type.String({ description: '表情ID' }),
|
||||
key: Type.String({ description: '表情key' }),
|
||||
summary: Type.String({ description: '表情摘要' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageMFace', description: '商城表情消息段' });
|
||||
|
||||
// 联系人消息接口定义
|
||||
export interface OB11MessageContact {
|
||||
type: OB11MessageDataType.contact;
|
||||
data: {
|
||||
type: 'qq' | 'group';
|
||||
id: string;
|
||||
};
|
||||
}
|
||||
// @消息段
|
||||
export const OB11MessageAtSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.at),
|
||||
data: Type.Object({
|
||||
qq: Type.String({ description: 'QQ号或all' }),
|
||||
name: Type.Optional(Type.String({ description: '显示名称' })),
|
||||
}),
|
||||
}, { $id: 'OB11MessageAt', description: '@消息段' });
|
||||
|
||||
// 文件消息基础接口定义
|
||||
export interface OB11MessageFileBase {
|
||||
data: {
|
||||
path?: string;
|
||||
thumb?: string;
|
||||
name?: string;
|
||||
file: string;
|
||||
url?: string;
|
||||
};
|
||||
}
|
||||
// 回复消息段
|
||||
export const OB11MessageReplySchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.reply),
|
||||
data: Type.Object({
|
||||
id: Type.Optional(Type.String({ description: '消息ID的短ID映射' })),
|
||||
seq: Type.Optional(Type.Number({ description: '消息序列号,优先使用' })),
|
||||
}),
|
||||
}, { $id: 'OB11MessageReply', description: '回复消息段' });
|
||||
|
||||
// 图片消息接口定义
|
||||
export interface OB11MessageImage extends OB11MessageFileBase {
|
||||
type: OB11MessageDataType.image;
|
||||
data: OB11MessageFileBase['data'] & {
|
||||
summary?: string; // 图片摘要
|
||||
sub_type?: PicSubType;
|
||||
};
|
||||
}
|
||||
// ==================== 文件类消息段 ====================
|
||||
|
||||
// 语音消息接口定义
|
||||
export interface OB11MessageRecord extends OB11MessageFileBase {
|
||||
type: OB11MessageDataType.voice;
|
||||
}
|
||||
// 文件消息段基础数据 Schema
|
||||
export const FileBaseDataSchema = Type.Object({
|
||||
file: Type.String({ description: '文件路径/URL/file:///' }),
|
||||
path: Type.Optional(Type.String({ description: '文件路径' })),
|
||||
url: Type.Optional(Type.String({ description: '文件URL' })),
|
||||
name: Type.Optional(Type.String({ description: '文件名' })),
|
||||
thumb: Type.Optional(Type.String({ description: '缩略图' })),
|
||||
}, { $id: 'FileBaseData', description: '文件消息段基础数据' });
|
||||
|
||||
// 文件消息接口定义
|
||||
export interface OB11MessageFile extends OB11MessageFileBase {
|
||||
type: OB11MessageDataType.file;
|
||||
}
|
||||
// 文件消息基础接口 Schema
|
||||
export const OB11MessageFileBaseSchema = Type.Object({
|
||||
data: FileBaseDataSchema,
|
||||
}, { $id: 'OB11MessageFileBase', description: '文件消息基础接口' });
|
||||
|
||||
// 视频消息接口定义
|
||||
export interface OB11MessageVideo extends OB11MessageFileBase {
|
||||
type: OB11MessageDataType.video;
|
||||
}
|
||||
// 图片消息段
|
||||
export const OB11MessageImageSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.image),
|
||||
data: Type.Intersect([
|
||||
FileBaseDataSchema,
|
||||
Type.Object({
|
||||
summary: Type.Optional(Type.String({ description: '图片摘要' })),
|
||||
sub_type: Type.Optional(Type.Number({ description: '图片子类型' })),
|
||||
}),
|
||||
]),
|
||||
}, { $id: 'OB11MessageImage', description: '图片消息段' });
|
||||
|
||||
// @消息接口定义
|
||||
export interface OB11MessageAt {
|
||||
type: OB11MessageDataType.at;
|
||||
data: {
|
||||
qq: string; // `${number}` | 'all'
|
||||
name?: string;
|
||||
};
|
||||
}
|
||||
// 语音消息段
|
||||
export const OB11MessageRecordSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.voice),
|
||||
data: FileBaseDataSchema,
|
||||
}, { $id: 'OB11MessageRecord', description: '语音消息段' });
|
||||
|
||||
// 回复消息接口定义
|
||||
export interface OB11MessageReply {
|
||||
type: OB11MessageDataType.reply;
|
||||
data: {
|
||||
id?: string; // msg_id 的短ID映射
|
||||
seq?: number; // msg_seq,优先使用
|
||||
};
|
||||
}
|
||||
// 视频消息段
|
||||
export const OB11MessageVideoSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.video),
|
||||
data: FileBaseDataSchema,
|
||||
}, { $id: 'OB11MessageVideo', description: '视频消息段' });
|
||||
|
||||
// 表情消息接口定义
|
||||
export interface OB11MessageFace {
|
||||
type: OB11MessageDataType.face;
|
||||
data: {
|
||||
id: string;
|
||||
resultId?: string;
|
||||
chainCount?: number;
|
||||
};
|
||||
}
|
||||
// 文件消息段
|
||||
export const OB11MessageFileSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.file),
|
||||
data: FileBaseDataSchema,
|
||||
}, { $id: 'OB11MessageFile', description: '文件消息段' });
|
||||
|
||||
// 混合消息类型定义
|
||||
export type OB11MessageMixType = OB11MessageData[] | string | OB11MessageData;
|
||||
// ==================== 音乐消息段 ====================
|
||||
|
||||
// 合并转发消息节点接口定义
|
||||
export interface OB11MessageNode {
|
||||
type: OB11MessageDataType.node;
|
||||
data: {
|
||||
id?: string;
|
||||
user_id?: number | string; // number
|
||||
uin?: number | string; // number, compatible with go-cqhttp
|
||||
nickname: string;
|
||||
name?: string; // compatible with go-cqhttp
|
||||
content: OB11MessageMixType;
|
||||
source?: string;
|
||||
news?: { text: string; }[];
|
||||
summary?: string;
|
||||
prompt?: string;
|
||||
time?: string;
|
||||
};
|
||||
}
|
||||
// ID音乐消息段数据
|
||||
const IdMusicDataSchema = Type.Object({
|
||||
type: Type.Union([
|
||||
Type.Literal('qq'),
|
||||
Type.Literal('163'),
|
||||
Type.Literal('kugou'),
|
||||
Type.Literal('migu'),
|
||||
Type.Literal('kuwo'),
|
||||
], { description: '音乐平台类型' }),
|
||||
id: Type.Union([Type.String(), Type.Number()], { description: '音乐ID' }),
|
||||
});
|
||||
|
||||
// 自定义音乐消息段数据
|
||||
const CustomMusicDataSchema = Type.Object({
|
||||
type: Type.Union([
|
||||
Type.Literal('qq'),
|
||||
Type.Literal('163'),
|
||||
Type.Literal('kugou'),
|
||||
Type.Literal('migu'),
|
||||
Type.Literal('kuwo'),
|
||||
Type.Literal('custom'),
|
||||
], { description: '音乐平台类型' }),
|
||||
id: Type.Undefined(),
|
||||
url: Type.String({ description: '点击后跳转URL' }),
|
||||
audio: Type.Optional(Type.String({ description: '音频URL' })),
|
||||
title: Type.Optional(Type.String({ description: '音乐标题' })),
|
||||
image: Type.String({ description: '封面图片URL' }),
|
||||
content: Type.Optional(Type.String({ description: '音乐简介' })),
|
||||
});
|
||||
|
||||
// ID音乐消息段
|
||||
export const OB11MessageIdMusicSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.music),
|
||||
data: IdMusicDataSchema,
|
||||
}, { $id: 'OB11MessageIdMusic', description: 'ID音乐消息段' });
|
||||
|
||||
// 自定义音乐消息段
|
||||
export const OB11MessageCustomMusicSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.music),
|
||||
data: CustomMusicDataSchema,
|
||||
}, { $id: 'OB11MessageCustomMusic', description: '自定义音乐消息段' });
|
||||
|
||||
// ==================== 特殊消息段 ====================
|
||||
|
||||
// 戳一戳消息段
|
||||
export const OB11MessagePokeSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.poke),
|
||||
data: Type.Object({
|
||||
type: Type.String({ description: '戳一戳类型' }),
|
||||
id: Type.String({ description: '戳一戳ID' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessagePoke', description: '戳一戳消息段' });
|
||||
|
||||
// 骰子消息段
|
||||
export const OB11MessageDiceSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.dice),
|
||||
data: Type.Object({
|
||||
result: Type.Union([Type.Number(), Type.String()], { description: '骰子结果' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageDice', description: '骰子消息段' });
|
||||
|
||||
// 猜拳消息段
|
||||
export const OB11MessageRPSSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.rps),
|
||||
data: Type.Object({
|
||||
result: Type.Union([Type.Number(), Type.String()], { description: '猜拳结果' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageRPS', description: '猜拳消息段' });
|
||||
|
||||
// 联系人消息段
|
||||
export const OB11MessageContactSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.contact),
|
||||
data: Type.Object({
|
||||
type: Type.Union([Type.Literal('qq'), Type.Literal('group')], { description: '联系人类型' }),
|
||||
id: Type.String({ description: '联系人ID' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageContact', description: '联系人消息段' });
|
||||
|
||||
// 位置消息段
|
||||
export const OB11MessageLocationSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.location),
|
||||
data: Type.Object({
|
||||
lat: Type.Union([Type.String(), Type.Number()], { description: '纬度' }),
|
||||
lon: Type.Union([Type.String(), Type.Number()], { description: '经度' }),
|
||||
title: Type.Optional(Type.String({ description: '标题' })),
|
||||
content: Type.Optional(Type.String({ description: '内容' })),
|
||||
}),
|
||||
}, { $id: 'OB11MessageLocation', description: '位置消息段' });
|
||||
|
||||
// ==================== 富文本消息段 ====================
|
||||
|
||||
// JSON消息段
|
||||
export const OB11MessageJsonSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.json),
|
||||
data: Type.Object({
|
||||
data: Type.Union([Type.String(), Type.Object({})], { description: 'JSON数据' }),
|
||||
config: Type.Optional(Type.Object({
|
||||
token: Type.String({ description: 'token' }),
|
||||
})),
|
||||
}),
|
||||
}, { $id: 'OB11MessageJson', description: 'JSON消息段' });
|
||||
|
||||
// XML消息段
|
||||
export const OB11MessageXmlSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.xml),
|
||||
data: Type.Object({
|
||||
data: Type.String({ description: 'XML数据' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageXml', description: 'XML消息段' });
|
||||
|
||||
// Markdown消息段
|
||||
export const OB11MessageMarkdownSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.markdown),
|
||||
data: Type.Object({
|
||||
content: Type.String({ description: 'Markdown内容' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageMarkdown', description: 'Markdown消息段' });
|
||||
|
||||
// 小程序消息段
|
||||
export const OB11MessageMiniAppSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.miniapp),
|
||||
data: Type.Object({
|
||||
data: Type.String({ description: '小程序数据' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageMiniApp', description: '小程序消息段' });
|
||||
|
||||
// ==================== 在线文件消息段 ====================
|
||||
|
||||
// 在线文件消息段
|
||||
export const OB11MessageOnlineFileSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.onlinefile),
|
||||
data: Type.Object({
|
||||
msgId: Type.String({ description: '消息ID' }),
|
||||
elementId: Type.String({ description: '元素ID' }),
|
||||
fileName: Type.String({ description: '文件名' }),
|
||||
fileSize: Type.String({ description: '文件大小' }),
|
||||
isDir: Type.Boolean({ description: '是否为目录' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageOnlineFile', description: '在线文件消息段' });
|
||||
|
||||
// 闪传消息段
|
||||
export const OB11MessageFlashTransferSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.flashtransfer),
|
||||
data: Type.Object({
|
||||
fileSetId: Type.String({ description: '文件集ID' }),
|
||||
}),
|
||||
}, { $id: 'OB11MessageFlashTransfer', description: 'QQ闪传消息段' });
|
||||
|
||||
// ==================== 合并转发消息段(递归类型)====================
|
||||
|
||||
// 由于 TypeBox 的递归类型限制,我们需要使用 Type.Recursive
|
||||
// 但为了与原始类型完全兼容,我们使用 Type.Unknown 来表示递归部分
|
||||
|
||||
// 合并转发消息节点
|
||||
export const OB11MessageNodeSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.node),
|
||||
data: Type.Object({
|
||||
id: Type.Optional(Type.String({ description: '转发消息ID' })),
|
||||
user_id: Type.Optional(Type.Union([Type.Number(), Type.String()], { description: '发送者QQ号' })),
|
||||
uin: Type.Optional(Type.Union([Type.Number(), Type.String()], { description: '发送者QQ号(兼容go-cqhttp)' })),
|
||||
nickname: Type.String({ description: '发送者昵称' }),
|
||||
name: Type.Optional(Type.String({ description: '发送者昵称(兼容go-cqhttp)' })),
|
||||
// content 使用 Any 以支持循环引用,实际类型是 OB11MessageMixType
|
||||
content: Type.Any({ description: '消息内容 (OB11MessageMixType)' }),
|
||||
source: Type.Optional(Type.String({ description: '消息来源' })),
|
||||
news: Type.Optional(Type.Array(Type.Object({
|
||||
text: Type.String({ description: '新闻文本' }),
|
||||
}))),
|
||||
summary: Type.Optional(Type.String({ description: '摘要' })),
|
||||
prompt: Type.Optional(Type.String({ description: '提示' })),
|
||||
time: Type.Optional(Type.String({ description: '时间' })),
|
||||
}),
|
||||
}, { $id: 'OB11MessageNode', description: '合并转发消息节点' });
|
||||
|
||||
// 合并转发消息段
|
||||
export const OB11MessageForwardSchema = Type.Object({
|
||||
type: Type.Literal(OB11MessageDataType.forward),
|
||||
data: Type.Object({
|
||||
id: Type.String({ description: '合并转发ID' }),
|
||||
// content 使用 Any 以支持类型兼容,实际类型是 OB11Message[]
|
||||
content: Type.Optional(Type.Any({ description: '消息内容 (OB11Message[])' })),
|
||||
}),
|
||||
}, { $id: 'OB11MessageForward', description: '合并转发消息段' });
|
||||
|
||||
// ==================== 消息段联合类型 ====================
|
||||
|
||||
// 所有消息段的联合类型(与原始 OB11MessageData 完全一致)
|
||||
export const OB11MessageDataSchema = Type.Union([
|
||||
OB11MessageTextSchema,
|
||||
OB11MessageFaceSchema,
|
||||
OB11MessageMFaceSchema,
|
||||
OB11MessageAtSchema,
|
||||
OB11MessageReplySchema,
|
||||
OB11MessageImageSchema,
|
||||
OB11MessageRecordSchema,
|
||||
OB11MessageVideoSchema,
|
||||
OB11MessageFileSchema,
|
||||
OB11MessageIdMusicSchema,
|
||||
OB11MessageCustomMusicSchema,
|
||||
OB11MessagePokeSchema,
|
||||
OB11MessageDiceSchema,
|
||||
OB11MessageRPSSchema,
|
||||
OB11MessageContactSchema,
|
||||
OB11MessageJsonSchema,
|
||||
OB11MessageMarkdownSchema,
|
||||
OB11MessageNodeSchema,
|
||||
OB11MessageForwardSchema,
|
||||
OB11MessageOnlineFileSchema,
|
||||
OB11MessageFlashTransferSchema,
|
||||
], { $id: 'OB11MessageData', description: 'OneBot 11 消息段' });
|
||||
|
||||
// 消息混合类型(数组、字符串或单个消息段)
|
||||
export const OB11MessageMixTypeSchema = Type.Union([
|
||||
Type.Array(OB11MessageDataSchema),
|
||||
Type.String(),
|
||||
OB11MessageDataSchema,
|
||||
], { $id: 'OB11MessageMixType', description: 'OneBot 11 消息混合类型' });
|
||||
|
||||
// ==================== 发送消息请求 ====================
|
||||
|
||||
// 发送消息请求
|
||||
export const OB11PostSendMsgSchema = Type.Object({
|
||||
message_type: Type.Optional(Type.Union([Type.Literal('private'), Type.Literal('group')], { description: '消息类型' })),
|
||||
user_id: Type.Optional(Type.String({ description: '用户QQ号' })),
|
||||
group_id: Type.Optional(Type.String({ description: '群号' })),
|
||||
message: OB11MessageMixTypeSchema,
|
||||
messages: Type.Optional(OB11MessageMixTypeSchema),
|
||||
auto_escape: Type.Optional(Type.Union([Type.Boolean(), Type.String()], { description: '是否作为纯文本发送' })),
|
||||
source: Type.Optional(Type.String({ description: '消息来源' })),
|
||||
news: Type.Optional(Type.Array(Type.Object({
|
||||
text: Type.String({ description: '文本' }),
|
||||
}))),
|
||||
summary: Type.Optional(Type.String({ description: '摘要' })),
|
||||
prompt: Type.Optional(Type.String({ description: '提示' })),
|
||||
time: Type.Optional(Type.String({ description: '时间' })),
|
||||
}, { $id: 'OB11PostSendMsg', description: 'OneBot 11 发送消息请求' });
|
||||
|
||||
// ==================== 完整消息对象 ====================
|
||||
|
||||
// 发送者信息 Schema(注意:OB11Sender 类型已在 data.ts 中定义,这里只提供 Schema)
|
||||
export const OB11SenderSchema = Type.Object({
|
||||
user_id: Type.Union([Type.Number(), Type.String()], { description: '发送者QQ号' }),
|
||||
nickname: Type.String({ description: '发送者昵称' }),
|
||||
card: Type.Optional(Type.String({ description: '群名片' })),
|
||||
role: Type.Optional(Type.String({ description: '角色' })),
|
||||
sex: Type.Optional(Type.String({ description: '性别' })),
|
||||
age: Type.Optional(Type.Number({ description: '年龄' })),
|
||||
area: Type.Optional(Type.String({ description: '地区' })),
|
||||
level: Type.Optional(Type.String({ description: '等级' })),
|
||||
title: Type.Optional(Type.String({ description: '头衔' })),
|
||||
}, { $id: 'OB11Sender', description: 'OneBot 11 发送者信息' });
|
||||
|
||||
// 完整消息对象
|
||||
export const OB11MessageSchema = Type.Object({
|
||||
real_seq: Type.Optional(Type.String({ description: '真实序列号' })),
|
||||
temp_source: Type.Optional(Type.Number({ description: '临时会话来源' })),
|
||||
message_sent_type: Type.Optional(Type.String({ description: '消息发送类型' })),
|
||||
target_id: Type.Optional(Type.Number({ description: '目标ID' })),
|
||||
self_id: Type.Optional(Type.Number({ description: '机器人QQ号' })),
|
||||
time: Type.Number({ description: '消息时间戳' }),
|
||||
message_id: Type.Number({ description: '消息ID' }),
|
||||
message_seq: Type.Number({ description: '消息序列号' }),
|
||||
real_id: Type.Number({ description: '真实ID' }),
|
||||
user_id: Type.Union([Type.Number(), Type.String()], { description: '发送者QQ号' }),
|
||||
group_id: Type.Optional(Type.Union([Type.Number(), Type.String()], { description: '群号' })),
|
||||
group_name: Type.Optional(Type.String({ description: '群名称' })),
|
||||
message_type: Type.Union([Type.Literal('private'), Type.Literal('group')], { description: '消息类型' }),
|
||||
sub_type: Type.Optional(Type.Union([
|
||||
Type.Literal('friend'),
|
||||
Type.Literal('group'),
|
||||
Type.Literal('normal'),
|
||||
], { description: '消息子类型' })),
|
||||
sender: OB11SenderSchema,
|
||||
message: Type.Union([Type.Array(OB11MessageDataSchema), Type.String()], { description: '消息内容' }),
|
||||
message_format: Type.Union([Type.Literal('array'), Type.Literal('string')], { description: '消息格式' }),
|
||||
raw_message: Type.String({ description: '原始消息' }),
|
||||
font: Type.Number({ description: '字体' }),
|
||||
post_type: Type.Optional(Type.String({ description: '上报类型' })),
|
||||
raw: Type.Optional(Type.Unknown({ description: '原始消息对象' })),
|
||||
emoji_likes_list: Type.Optional(Type.Array(Type.Object({
|
||||
emoji_id: Type.String({ description: '表情ID' }),
|
||||
emoji_type: Type.String({ description: '表情类型' }),
|
||||
likes_cnt: Type.String({ description: '点赞数' }),
|
||||
}), { description: '表情点赞列表' })),
|
||||
}, { $id: 'OB11Message', description: 'OneBot 11 完整消息对象' });
|
||||
|
||||
// ==================== TypeScript 类型导出 ====================
|
||||
|
||||
export type OB11MessageText = Static<typeof OB11MessageTextSchema>;
|
||||
export type OB11MessageFace = Static<typeof OB11MessageFaceSchema>;
|
||||
export type OB11MessageMFace = Static<typeof OB11MessageMFaceSchema>;
|
||||
export type OB11MessageAt = Static<typeof OB11MessageAtSchema>;
|
||||
export type OB11MessageReply = Static<typeof OB11MessageReplySchema>;
|
||||
export type OB11MessageFileBase = Static<typeof OB11MessageFileBaseSchema>;
|
||||
export type OB11MessageImage = Static<typeof OB11MessageImageSchema>;
|
||||
export type OB11MessageRecord = Static<typeof OB11MessageRecordSchema>;
|
||||
export type OB11MessageVideo = Static<typeof OB11MessageVideoSchema>;
|
||||
export type OB11MessageFile = Static<typeof OB11MessageFileSchema>;
|
||||
export type OB11MessageIdMusic = Static<typeof OB11MessageIdMusicSchema>;
|
||||
export type OB11MessageCustomMusic = Static<typeof OB11MessageCustomMusicSchema>;
|
||||
export type OB11MessagePoke = Static<typeof OB11MessagePokeSchema>;
|
||||
export type OB11MessageDice = Static<typeof OB11MessageDiceSchema>;
|
||||
export type OB11MessageRPS = Static<typeof OB11MessageRPSSchema>;
|
||||
export type OB11MessageContact = Static<typeof OB11MessageContactSchema>;
|
||||
export type OB11MessageLocation = Static<typeof OB11MessageLocationSchema>;
|
||||
export type OB11MessageJson = Static<typeof OB11MessageJsonSchema>;
|
||||
export type OB11MessageXml = Static<typeof OB11MessageXmlSchema>;
|
||||
export type OB11MessageMarkdown = Static<typeof OB11MessageMarkdownSchema>;
|
||||
export type OB11MessageMiniApp = Static<typeof OB11MessageMiniAppSchema>;
|
||||
export type OB11MessageNode = Static<typeof OB11MessageNodeSchema>;
|
||||
export type OB11MessageForward = Static<typeof OB11MessageForwardSchema>;
|
||||
export type OB11MessageOnlineFile = Static<typeof OB11MessageOnlineFileSchema>;
|
||||
export type OB11MessageFlashTransfer = Static<typeof OB11MessageFlashTransferSchema>;
|
||||
export type OB11MessageData = Static<typeof OB11MessageDataSchema>;
|
||||
export type OB11MessageMixType = Static<typeof OB11MessageMixTypeSchema>;
|
||||
export type OB11PostSendMsg = Static<typeof OB11PostSendMsgSchema>;
|
||||
// 注意:OB11Sender 类型已在 data.ts 中定义,避免重复导出
|
||||
// export type OB11Sender = Static<typeof OB11SenderSchema>;
|
||||
export type OB11Message = Static<typeof OB11MessageSchema>;
|
||||
|
||||
// 合并转发消息节点纯文本接口定义
|
||||
export type OB11MessageNodePlain = OB11MessageNode & {
|
||||
@@ -205,101 +450,28 @@ export type OB11MessageNodePlain = OB11MessageNode & {
|
||||
};
|
||||
};
|
||||
|
||||
// 音乐消息接口定义
|
||||
export interface OB11MessageIdMusic {
|
||||
type: OB11MessageDataType.music;
|
||||
data: IdMusicSignPostData;
|
||||
}
|
||||
|
||||
// 自定义音乐消息接口定义
|
||||
export interface OB11MessageCustomMusic {
|
||||
type: OB11MessageDataType.music;
|
||||
data: Omit<CustomMusicSignPostData, 'singer'> & { content?: string; };
|
||||
}
|
||||
|
||||
// JSON消息接口定义
|
||||
export interface OB11MessageJson {
|
||||
type: OB11MessageDataType.json;
|
||||
data: { config?: { token: string; }, data: string | object; };
|
||||
}
|
||||
|
||||
// 骰子消息接口定义
|
||||
export interface OB11MessageDice {
|
||||
type: OB11MessageDataType.dice;
|
||||
data: {
|
||||
result: number /* intended */ | string /* in fact */;
|
||||
};
|
||||
}
|
||||
|
||||
// 猜拳消息接口定义
|
||||
export interface OB11MessageRPS {
|
||||
type: OB11MessageDataType.rps;
|
||||
data: {
|
||||
result: number | string;
|
||||
};
|
||||
}
|
||||
|
||||
// Markdown消息接口定义
|
||||
export interface OB11MessageMarkdown {
|
||||
type: OB11MessageDataType.markdown;
|
||||
data: {
|
||||
content: string;
|
||||
};
|
||||
// 返回数据接口定义
|
||||
export interface OB11Return<DataType> {
|
||||
status: string;
|
||||
retcode: number;
|
||||
data: DataType;
|
||||
message: string;
|
||||
echo?: unknown; // ws调用api才有此字段
|
||||
wording?: string; // go-cqhttp字段,错误信息
|
||||
stream?: 'stream-action' | 'normal-action'; // 流式返回标记
|
||||
}
|
||||
|
||||
// 合并转发消息接口定义
|
||||
export interface OB11MessageForward {
|
||||
type: OB11MessageDataType.forward;
|
||||
data: {
|
||||
id: string;
|
||||
content?: OB11Message[];
|
||||
};
|
||||
export interface OB11ForwardMessage extends OB11Message {
|
||||
content: OB11MessageData[] | string;
|
||||
}
|
||||
|
||||
export interface OB11MessageOnlineFile {
|
||||
type: OB11MessageDataType.onlinefile;
|
||||
data: {
|
||||
msgId: string;
|
||||
elementId: string;
|
||||
fileName: string;
|
||||
fileSize: string;
|
||||
isDir: boolean;
|
||||
}
|
||||
// 消息类型枚举
|
||||
export enum OB11MessageType {
|
||||
private = 'private',
|
||||
group = 'group',
|
||||
}
|
||||
|
||||
export interface OB11MessageFlashTransfer {
|
||||
type: OB11MessageDataType.flashtransfer;
|
||||
data: {
|
||||
fileSetId: string;
|
||||
}
|
||||
}
|
||||
|
||||
// 消息数据类型定义
|
||||
export type OB11MessageData =
|
||||
OB11MessageText |
|
||||
OB11MessageFace | OB11MessageMFace |
|
||||
OB11MessageAt | OB11MessageReply |
|
||||
OB11MessageImage | OB11MessageRecord | OB11MessageFile | OB11MessageVideo |
|
||||
OB11MessageNode | OB11MessageIdMusic | OB11MessageCustomMusic | OB11MessageJson |
|
||||
OB11MessageDice | OB11MessageRPS | OB11MessageMarkdown | OB11MessageForward | OB11MessageContact |
|
||||
OB11MessagePoke | OB11MessageOnlineFile | OB11MessageFlashTransfer;
|
||||
|
||||
// 发送消息接口定义
|
||||
export interface OB11PostSendMsg {
|
||||
message_type?: 'private' | 'group';
|
||||
user_id?: string;
|
||||
group_id?: string;
|
||||
message: OB11MessageMixType;
|
||||
messages?: OB11MessageMixType;
|
||||
auto_escape?: boolean | string;
|
||||
source?: string;
|
||||
news?: { text: string; }[];
|
||||
summary?: string;
|
||||
prompt?: string;
|
||||
time?: string;
|
||||
}
|
||||
|
||||
// 上下文接口定义
|
||||
export interface OB11PostContext {
|
||||
message_type?: 'private' | 'group';
|
||||
user_id?: string;
|
||||
|
||||
Reference in new issue
Block a user