mioku-plugin-chat 2.3.1 → 2.4.0

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.
@@ -1,6 +1,7 @@
1
1
  import type { AIInstance } from "mioku";
2
2
  import type { MiokiContext } from "mioki";
3
3
  import { logger } from "mioki";
4
+ import { prepareImageUrlsForModel } from "./media/image-compress";
4
5
 
5
6
  /**
6
7
  * 多模态内容管理器
@@ -47,6 +48,8 @@ export async function describeImage(
47
48
  }
48
49
  }
49
50
 
51
+ imageUrls = await prepareImageUrlsForModel(imageUrls);
52
+
50
53
  // 构建提示词
51
54
  const systemPrompt = `You are an image analysis assistant. Your task is to provide a detailed, accurate description of the image.
52
55
 
@@ -110,36 +113,45 @@ ${context ? `\nUser context: ${context}` : ""}`;
110
113
  }
111
114
  }
112
115
 
116
+ export type MediaByMessageId =
117
+ | { kind: "image"; url: string }
118
+ | { kind: "video"; sources: string[] }
119
+ | null;
120
+
113
121
  /**
114
- * 获取指定消息 ID 中的图片 URL
122
+ * 获取指定消息 ID 中的首个媒体(图片或视频)
115
123
  * @param ctx Mioki 上下文
116
124
  * @param messageId 消息 ID
117
125
  * @param e
118
- * @returns 图片 URL 或 null
126
+ * @returns 媒体信息或 null
119
127
  */
120
- export async function getImageUrlByMessageId(
128
+ export async function getMediaByMessageId(
121
129
  ctx: MiokiContext,
122
130
  messageId: number,
123
131
  e: any,
124
- ): Promise<string | null> {
132
+ ): Promise<MediaByMessageId> {
125
133
  try {
126
- // 通过 message_id 获取消息详情
127
134
  const msg = await ctx.pickBot(e.self_id).getMsg(messageId);
128
135
  if (!msg || !msg.message) {
129
136
  return null;
130
137
  }
131
138
 
132
- // 查找图片段
133
- const imageSeg = msg.message.find((s: any) => s.type === "image");
134
- if (!imageSeg) {
135
- return null;
139
+ for (const seg of msg.message as any[]) {
140
+ if (seg.type === "image") {
141
+ const url = seg.url || seg.data?.url;
142
+ if (url) return { kind: "image", url };
143
+ }
144
+ if (seg.type === "video") {
145
+ const { getSegmentSourceCandidates } = await import("./media/history-media");
146
+ const sources = getSegmentSourceCandidates(seg);
147
+ if (sources.length > 0) return { kind: "video", sources };
148
+ }
136
149
  }
137
150
 
138
- // 提取图片 URL
139
- return (imageSeg as any).url || (imageSeg as any).data?.url || null;
151
+ return null;
140
152
  } catch (err) {
141
153
  logger.error(
142
- `[multimodal] Failed to get image URL from message ${messageId}: ${err}`,
154
+ `[multimodal] Failed to get media from message ${messageId}: ${err}`,
143
155
  );
144
156
  return null;
145
157
  }