kokkoro-plugin-animetrace 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -12,6 +12,16 @@ bun add kokkoro-plugin-animetrace
12
12
 
13
13
  完整使用说明见 [AnimeTrace 角色识别](https://kokkoro.js.org/plugin/animetrace)。
14
14
 
15
+ ## 环境变量
16
+
17
+ 在项目根目录的 `.env` 中设置每个人物展示的候选角色数量上限:
18
+
19
+ ```ini
20
+ ANIMETRACE_LIMIT=3
21
+ ```
22
+
23
+ `ANIMETRACE_LIMIT` 必须为正整数,默认值为 `3`。
24
+
15
25
  ## API
16
26
 
17
27
  通过 `service` 入口导入 `fetchCharacters()` 获取完整响应,通过 `util` 入口导入 `createMarkdown()` 生成 QQ Markdown:
@@ -24,4 +34,6 @@ const { data } = await fetchCharacters('https://kokkoro.js.org/logo.png');
24
34
  const markdown = createMarkdown(data);
25
35
  ```
26
36
 
37
+ `fetchCharacters()` 的第二个参数可传入日志记录器,记录完整请求参数和接口响应。
38
+
27
39
  `service` 入口同时导出 `ANIMETRACE_API`、`AnimeTrace` 和 `CharacterResult`。参数、响应字段和错误行为见 [插件 API 文档](https://kokkoro.js.org/plugin/animetrace#api)。
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kokkoro-plugin-animetrace",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "AnimeTrace 角色识别,查找图片中的动漫、游戏角色及所属作品。",
5
5
  "keywords": [
6
6
  "animetrace",
package/src/index.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { useCommand, useLogger } from '@kokkoro/core';
2
2
 
3
- import { ANIMETRACE_API, fetchCharacters } from './service';
3
+ import { fetchCharacters } from './service';
4
4
  import { createMarkdown } from './util';
5
5
 
6
6
  const logger = useLogger();
@@ -12,20 +12,14 @@ export default () => {
12
12
  if (!image?.url) {
13
13
  throw new Error('请在指令中附带需要识别的图片');
14
14
  }
15
- logger.debug('发送 AnimeTrace 请求', {
16
- method: 'POST',
17
- url: ANIMETRACE_API,
18
- payload: { url: image.url, is_multi: 1, ai_detect: 0 },
19
- });
20
-
21
- const result = await fetchCharacters(image.url);
15
+ const result = await fetchCharacters(image.url, logger);
16
+ const { data, trace_id } = result;
22
17
 
23
- logger.debug('收到 AnimeTrace 响应', result);
24
- logger.info('已识别图片中的角色', { count: result.data.length, trace_id: result.trace_id });
18
+ logger.info('已识别图片中的角色', { count: data.length, trace_id });
25
19
 
26
20
  await context.reply({
27
21
  msg_type: 2,
28
- markdown: { content: createMarkdown(result.data) },
22
+ markdown: { content: createMarkdown(data) },
29
23
  });
30
24
  }).shortcut('搜角色');
31
25
  };
package/src/service.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { type Logger } from '@kokkoro/core';
2
+
1
3
  /** AnimeTrace v1 识别接口的请求地址。 */
2
4
  export const ANIMETRACE_API = 'https://api.animetrace.com/v1/search';
3
5
 
@@ -11,9 +13,9 @@ export interface AnimeTrace {
11
13
  readonly code: number;
12
14
  /** 是否判定为 AI 生成图片。 */
13
15
  readonly ai: boolean;
14
- /** 本次识别的唯一 ID。 */
16
+ /** 本次识别的唯一 ID,反馈问题时使用。 */
15
17
  readonly trace_id: string;
16
- /** 图片中各个人物的识别结果。 */
18
+ /** 检测到的每个人物的结果列表。 */
17
19
  readonly data: readonly CharacterResult[];
18
20
  }
19
21
 
@@ -21,11 +23,11 @@ export interface AnimeTrace {
21
23
  export interface CharacterResult {
22
24
  /** 人物位置 `[x1, y1, x2, y2]`,坐标相对于图片宽高,范围为 0 到 1。 */
23
25
  readonly box: readonly [number, number, number, number];
24
- /** 检测框的唯一 ID。 */
26
+ /** 每个检测框的唯一 ID。 */
25
27
  readonly box_id: string;
26
- /** 是否置信度较低,需要人工确认。 */
28
+ /** 置信度较低,候选较多,需人工确认。 */
27
29
  readonly not_confident: boolean;
28
- /** 候选角色,越靠前可能性越大。 */
30
+ /** 候选角色列表,越靠前可能性越大。 */
29
31
  readonly character: readonly {
30
32
  /** 作品名称。 */
31
33
  readonly work: string;
@@ -35,12 +37,18 @@ export interface CharacterResult {
35
37
  }
36
38
 
37
39
  /** 使用 AnimeTrace 默认模型识别图片中的角色,并返回完整的接口响应。 */
38
- export async function fetchCharacters(url: string): Promise<AnimeTrace> {
40
+ export async function fetchCharacters(url: string, logger?: Logger): Promise<AnimeTrace> {
41
+ const payload = { url, is_multi: '1', ai_detect: '0' };
39
42
  const form = new FormData();
40
43
 
41
- form.set('url', url);
42
- form.set('is_multi', '1');
43
- form.set('ai_detect', '0');
44
+ for (const [key, value] of Object.entries(payload)) {
45
+ form.set(key, value);
46
+ }
47
+ logger?.debug('发送 AnimeTrace 请求', {
48
+ method: 'POST',
49
+ url: ANIMETRACE_API,
50
+ payload,
51
+ });
44
52
 
45
53
  const response = await fetch(ANIMETRACE_API, { method: 'POST', body: form });
46
54
 
@@ -49,6 +57,8 @@ export async function fetchCharacters(url: string): Promise<AnimeTrace> {
49
57
  }
50
58
  const result = <AnimeTrace>await response.json();
51
59
 
60
+ logger?.debug('收到 AnimeTrace 响应', result);
61
+
52
62
  if (result.code !== 0) {
53
63
  throw new Error(`AnimeTrace 识别失败,状态码 ${result.code}`);
54
64
  }
package/src/util.ts CHANGED
@@ -1,18 +1,29 @@
1
1
  import { type CharacterResult } from './service';
2
2
 
3
+ const { ANIMETRACE_LIMIT = 3 } = import.meta.env;
4
+ const limit = Number(ANIMETRACE_LIMIT);
5
+
6
+ if (!Number.isInteger(limit) || limit <= 0) {
7
+ throw new Error('ANIMETRACE_LIMIT 必须为正整数');
8
+ }
9
+
3
10
  /** 将 AnimeTrace 识别结果转换为 QQ Markdown。 */
4
11
  export function createMarkdown(results: readonly CharacterResult[]): string {
5
12
  const lines = ['## AnimeTrace 搜索结果'];
6
13
 
14
+ if (results.some(({ not_confident: isNotConfident }) => isNotConfident)) {
15
+ lines.push('', '> 带 * 的人物可能识别不准确。');
16
+ }
17
+
7
18
  for (const [index, { character: candidates, not_confident: isNotConfident }] of results.entries()) {
8
- if (results.length > 1) {
9
- lines.push(`**人物 ${index + 1}**`);
10
- }
11
- lines.push(candidates.map(({ character, work }) => `- **${character}** \n ${work}`).join('\n'));
19
+ const prefix = results.length === 1 && isNotConfident ? '* ' : '';
12
20
 
13
- if (isNotConfident) {
14
- lines.push('> 识别置信度较低,请结合原图确认。');
21
+ lines.push('');
22
+
23
+ if (results.length > 1) {
24
+ lines.push(`**${isNotConfident ? '* ' : ''}人物 ${index + 1}**`);
15
25
  }
26
+ lines.push(...candidates.slice(0, limit).map(({ character, work }) => `- **${prefix}${character}** \n ${work}`));
16
27
  }
17
- return lines.join('\n\n');
28
+ return lines.join('\n');
18
29
  }