mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-13 00:10:27 +00:00
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.
57 lines
1.9 KiB
TypeScript
57 lines
1.9 KiB
TypeScript
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
|
|
import { ActionName } from '@/napcat-onebot/action/router';
|
|
import { checkFileExist, uriToLocalFile } from 'napcat-common/src/file';
|
|
import fs from 'fs';
|
|
import { Static, Type } from '@sinclair/typebox';
|
|
|
|
import { ExtendsActionsExamples } from './examples';
|
|
|
|
const PayloadSchema = Type.Object({
|
|
image: Type.String({ description: '图片路径、URL或Base64' }),
|
|
});
|
|
|
|
type PayloadType = Static<typeof PayloadSchema>;
|
|
|
|
const ReturnSchema = Type.Any({ description: 'OCR结果' });
|
|
|
|
type ReturnType = Static<typeof ReturnSchema>;
|
|
|
|
class OCRImageBase extends OneBotAction<PayloadType, ReturnType> {
|
|
override payloadSchema = PayloadSchema;
|
|
override returnSchema = ReturnSchema;
|
|
override actionSummary = '图片 OCR 识别';
|
|
override actionDescription = '识别图片中的文字内容';
|
|
override actionTags = ['扩展接口'];
|
|
override payloadExample = ExtendsActionsExamples.OCRImage.payload;
|
|
|
|
async _handle (payload: PayloadType): Promise<ReturnType> {
|
|
const { path, success } = await uriToLocalFile(this.core.NapCatTempPath, payload.image);
|
|
if (!success) {
|
|
throw new Error(`OCR ${payload.image}失败, image字段可能格式不正确`);
|
|
}
|
|
if (path) {
|
|
try {
|
|
await checkFileExist(path, 5000); // 避免崩溃
|
|
const ret = await this.core.apis.SystemApi.ocrImage(path);
|
|
if (!ret) {
|
|
throw new Error(`OCR ${payload.image}失败`);
|
|
}
|
|
return ret.result;
|
|
} finally {
|
|
fs.unlink(path, () => { });
|
|
}
|
|
}
|
|
throw new Error(`OCR ${payload.image}失败, 文件可能不存在`);
|
|
}
|
|
}
|
|
|
|
export class OCRImage extends OCRImageBase {
|
|
override actionName = ActionName.OCRImage;
|
|
override actionSummary = '图片 OCR 识别';
|
|
}
|
|
|
|
export class IOCRImage extends OCRImageBase {
|
|
override actionName = ActionName.IOCRImage;
|
|
override actionSummary = '图片 OCR 识别 (内部)';
|
|
}
|