feat(onebot): implement ocr_image via OIDB 0xE07_0

Server-side image OCR ported from Lagrange.Core ([Service]
"OidbSvcTrpcTcp.0xe07_0") cross-checked with NapCat's Oidb.0xE07 proto
(NapCat's transformer cmd 0xEB7 is a typo; the proto name + Lagrange + the
wire name all say 0xE07_0). New ImageOcr service + proto types, misc.ocrImage
api, and the ocr_image/.ocr_image action. The cmd takes an image URL, so the
action accepts an http(s) url or a cached image file_id (resolved via
getImageInfo); base64/local input is unsupported (NapCat's path is a
Windows-only kernel OCR, not portable). Wire round-trip + response-parse
tests lock the pb tags.
This commit is contained in:
DrSmoothl committed 2026-06-18 00:36:07 +08:00
1 parent f936515a59
commit e07090376a
8 files changed
+271 -3

No files matched your search

+25 -3
View File
@@ -1248,12 +1248,34 @@ export const actions = [
},
}),
// ocr_image — 服务端 OCR via OIDB 0xE07_0(port 自 Lagrange/NapCat proto)。
// 该 cmd 接收图片 URL(服务端拉取),故 image 支持:http(s) URL 直用,或
// 已缓存图片的 file_id(经 getImageInfo 解析出 URL)。base64/本地文件需先
// 上传换 URL,本实现不覆盖(NapCat 走的是 Windows-only 内核 OCR,不可移植)。
defineAction({
name: ['ocr_image', '.ocr_image'],
summary: 'OCR 图片(未实现)',
summary: 'OCR 图片(服务端,需图片 URL 或已缓存的图片 file_id)',
readOnly: true,
params: {},
run: () => failedResponse(RETCODE.ACTION_FAILED, 'not yet implemented'),
params: { image: f.string({ allowEmpty: false }) },
run: async (p, ctx) => {
let url = /^https?:\/\//i.test(p.image) ? p.image : '';
if (!url) {
const info = await ctx.getImageInfo(p.image);
const resolved = info && typeof info.url === 'string' ? info.url : '';
if (resolved) url = resolved;
}
if (!url) {
return failedResponse(
RETCODE.ACTION_FAILED,
'ocr_image needs an http(s) image url or a cached image file_id; '
+ 'base64/local-file input is not supported',
);
}
const result = await ctx.bridge.apis.misc.ocrImage(url);
// OcrResult is plain JSON data; the interface just lacks an index
// signature, so coerce for the JsonValue-typed response.
return okResponse(result as unknown as JsonObject);
},
}),
groupAction({
@@ -751,3 +751,48 @@ describe('extended-actions / get_rkey_server', () => {
expect(res).toMatchObject({ status: 'failed', retcode: 100 });
});
});
// ─── Wave 3: ocr_image / .ocr_image (OIDB 0xE07_0) ───
describe('extended-actions / ocr_image', () => {
const ocrResult = { texts: [{ text: 'hello', confidence: 99, coordinates: [{ x: 1, y: 2 }] }], language: 'en' };
it('OCRs an http(s) image URL directly', async () => {
const ocrImage = vi.fn(async () => ocrResult);
const bridge = fakeBridge({ apis: { misc: { ocrImage } } });
const res = await makeHandler(fakeCtx(bridge)).handle('ocr_image', { image: 'https://x/a.jpg' });
expect(res).toMatchObject({ status: 'ok', data: ocrResult });
expect(ocrImage).toHaveBeenCalledWith('https://x/a.jpg');
});
it('resolves a cached image file_id to a url via getImageInfo', async () => {
const ocrImage = vi.fn(async () => ocrResult);
const getImageInfo = vi.fn(async () => ({ url: 'https://cdn/resolved.jpg' }));
const bridge = fakeBridge({ apis: { misc: { ocrImage } } });
const res = await makeHandler(fakeCtx(bridge, { getImageInfo })).handle('ocr_image', { image: 'abc.jpg' });
expect(res.status).toBe('ok');
expect(ocrImage).toHaveBeenCalledWith('https://cdn/resolved.jpg');
});
it('.ocr_image shares the same handler', async () => {
const ocrImage = vi.fn(async () => ocrResult);
const bridge = fakeBridge({ apis: { misc: { ocrImage } } });
const res = await makeHandler(fakeCtx(bridge)).handle('.ocr_image', { image: 'http://x/a.jpg' });
expect(res.status).toBe('ok');
});
it('fails when the id cannot be resolved to a url', async () => {
const ocrImage = vi.fn();
const getImageInfo = vi.fn(async () => null);
const bridge = fakeBridge({ apis: { misc: { ocrImage } } });
const res = await makeHandler(fakeCtx(bridge, { getImageInfo })).handle('ocr_image', { image: 'unknown' });
expect(res.status).toBe('failed');
expect(ocrImage).not.toHaveBeenCalled();
});
it('rejects missing image', async () => {
const bridge = fakeBridge({ apis: { misc: { ocrImage: vi.fn() } } });
const res = await makeHandler(fakeCtx(bridge)).handle('ocr_image', {});
expect(res).toMatchObject({ status: 'failed', retcode: 1400 });
});
});