mioku-plugin-music 3.1.0 → 3.1.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 +1 -0
- package/package.json +1 -1
- package/runtime-core/audio-file.ts +67 -0
- package/runtime-core/message.ts +35 -33
- package/runtime-core/service.ts +69 -8
- package/skills/music.ts +1 -1
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
|
|
5
|
+
const SEND_DIR = path.join(process.cwd(), "temp", "music", "send");
|
|
6
|
+
const MAX_BASE_LENGTH = 80;
|
|
7
|
+
const FALLBACK_BASE = "song";
|
|
8
|
+
|
|
9
|
+
function sanitizeBaseName(value: string): string {
|
|
10
|
+
return value
|
|
11
|
+
.replace(/[\\/:*?"<>|\u0000-\u001f]/g, "_")
|
|
12
|
+
.replace(/\s+/g, " ")
|
|
13
|
+
.slice(0, MAX_BASE_LENGTH)
|
|
14
|
+
.replace(/^[.\s]+|[.\s]+$/g, "")
|
|
15
|
+
.trim();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** 由歌名与歌手生成发送用文件名(不含扩展名) */
|
|
19
|
+
export function buildTrackBaseName(
|
|
20
|
+
track: { title?: string; artist?: string },
|
|
21
|
+
fallback: string = FALLBACK_BASE,
|
|
22
|
+
): string {
|
|
23
|
+
const label = [track.title, track.artist]
|
|
24
|
+
.map((part) => String(part ?? "").trim())
|
|
25
|
+
.filter((part) => part.length > 0)
|
|
26
|
+
.join(" - ");
|
|
27
|
+
return sanitizeBaseName(label) || sanitizeBaseName(fallback) || FALLBACK_BASE;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface PreparedAudioFile {
|
|
31
|
+
readonly filePath: string;
|
|
32
|
+
readonly fileName: string;
|
|
33
|
+
cleanup(): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 按歌名生成发送副本,避免把缓存文件名(如 netease-<id>.mp3)直接发出去 */
|
|
37
|
+
export async function prepareTrackFile(options: {
|
|
38
|
+
sourcePath: string;
|
|
39
|
+
title?: string;
|
|
40
|
+
artist?: string;
|
|
41
|
+
fallbackName?: string;
|
|
42
|
+
}): Promise<PreparedAudioFile> {
|
|
43
|
+
const ext = path.extname(options.sourcePath);
|
|
44
|
+
const fileName = `${buildTrackBaseName(options, options.fallbackName)}${ext}`;
|
|
45
|
+
const dir = path.join(SEND_DIR, randomUUID());
|
|
46
|
+
const filePath = path.join(dir, fileName);
|
|
47
|
+
|
|
48
|
+
await fs.mkdir(dir, { recursive: true });
|
|
49
|
+
try {
|
|
50
|
+
await fs.link(options.sourcePath, filePath);
|
|
51
|
+
} catch {
|
|
52
|
+
try {
|
|
53
|
+
await fs.copyFile(options.sourcePath, filePath);
|
|
54
|
+
} catch (error) {
|
|
55
|
+
await fs.rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
56
|
+
throw error;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
filePath,
|
|
62
|
+
fileName,
|
|
63
|
+
async cleanup(): Promise<void> {
|
|
64
|
+
await fs.rm(dir, { recursive: true, force: true }).catch(() => {});
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
}
|
package/runtime-core/message.ts
CHANGED
|
@@ -85,6 +85,21 @@ export async function sendImageMessage(
|
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
+
function errorText(error: unknown): string {
|
|
89
|
+
const text = error instanceof Error ? error.message : String(error ?? "");
|
|
90
|
+
return text.trim().slice(0, 300) || "未知错误";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function isTimeoutError(error: unknown): boolean {
|
|
94
|
+
const text = errorText(error);
|
|
95
|
+
return (
|
|
96
|
+
text.includes("timeout") ||
|
|
97
|
+
text.includes("超时") ||
|
|
98
|
+
text.includes("timed out") ||
|
|
99
|
+
text.includes("ETIMEDOUT")
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
88
103
|
export async function sendFileMessage(
|
|
89
104
|
ctx: MiokuContext,
|
|
90
105
|
event: MessageEvent,
|
|
@@ -92,7 +107,10 @@ export async function sendFileMessage(
|
|
|
92
107
|
name: string,
|
|
93
108
|
): Promise<void> {
|
|
94
109
|
const ext = path.extname(filePath);
|
|
95
|
-
const fileName =
|
|
110
|
+
const fileName =
|
|
111
|
+
!ext || name.toLowerCase().endsWith(ext.toLowerCase())
|
|
112
|
+
? name
|
|
113
|
+
: `${name}${ext}`;
|
|
96
114
|
|
|
97
115
|
const send = async (source: string): Promise<void> => {
|
|
98
116
|
await sendSegments(ctx, event, [
|
|
@@ -103,42 +121,34 @@ export async function sendFileMessage(
|
|
|
103
121
|
const canReadLocalFile =
|
|
104
122
|
filePath.startsWith("/") || /^[A-Za-z]:[\\/]/.test(filePath);
|
|
105
123
|
|
|
106
|
-
let
|
|
124
|
+
let pathError: unknown;
|
|
107
125
|
try {
|
|
108
126
|
await send(filePath);
|
|
109
127
|
return;
|
|
110
128
|
} catch (error) {
|
|
111
|
-
|
|
129
|
+
pathError = error;
|
|
112
130
|
}
|
|
113
131
|
|
|
114
132
|
if (!canReadLocalFile) {
|
|
115
|
-
throw
|
|
116
|
-
? fileSendError
|
|
117
|
-
: new Error(String(fileSendError || "文件发送失败"));
|
|
133
|
+
throw new Error(`文件发送失败:${errorText(pathError)}`);
|
|
118
134
|
}
|
|
119
135
|
|
|
120
136
|
let buffer: Buffer;
|
|
121
137
|
try {
|
|
122
138
|
buffer = await fs.readFile(filePath);
|
|
123
139
|
} catch {
|
|
124
|
-
throw
|
|
125
|
-
? fileSendError
|
|
126
|
-
: new Error(String(fileSendError || "文件发送失败"));
|
|
140
|
+
throw new Error(`文件发送失败:${errorText(pathError)}`);
|
|
127
141
|
}
|
|
128
142
|
|
|
129
143
|
try {
|
|
130
144
|
await send(`base64://${buffer.toString("base64")}`);
|
|
131
145
|
} catch (base64Error) {
|
|
132
|
-
|
|
133
|
-
const timeoutLike =
|
|
134
|
-
message.includes("timeout") ||
|
|
135
|
-
message.includes("超时") ||
|
|
136
|
-
message.includes("timed out") ||
|
|
137
|
-
message.includes("ETIMEDOUT");
|
|
138
|
-
if (timeoutLike) {
|
|
146
|
+
if (isTimeoutError(base64Error)) {
|
|
139
147
|
return;
|
|
140
148
|
}
|
|
141
|
-
throw new Error(
|
|
149
|
+
throw new Error(
|
|
150
|
+
`文件发送失败:路径发送失败(${errorText(pathError)});base64 发送失败(${errorText(base64Error)})`,
|
|
151
|
+
);
|
|
142
152
|
}
|
|
143
153
|
}
|
|
144
154
|
|
|
@@ -156,41 +166,33 @@ export async function sendRecordMessage(
|
|
|
156
166
|
const canReadLocalFile =
|
|
157
167
|
audioPath.startsWith("/") || /^[A-Za-z]:[\\/]/.test(audioPath);
|
|
158
168
|
|
|
159
|
-
let
|
|
169
|
+
let pathError: unknown;
|
|
160
170
|
try {
|
|
161
171
|
await send(audioPath);
|
|
162
172
|
return;
|
|
163
173
|
} catch (error) {
|
|
164
|
-
|
|
174
|
+
pathError = error;
|
|
165
175
|
}
|
|
166
176
|
|
|
167
177
|
if (!canReadLocalFile) {
|
|
168
|
-
throw
|
|
169
|
-
? fileSendError
|
|
170
|
-
: new Error(String(fileSendError || "语音发送失败"));
|
|
178
|
+
throw new Error(`语音发送失败:${errorText(pathError)}`);
|
|
171
179
|
}
|
|
172
180
|
|
|
173
181
|
let buffer: Buffer;
|
|
174
182
|
try {
|
|
175
183
|
buffer = await fs.readFile(audioPath);
|
|
176
184
|
} catch {
|
|
177
|
-
throw
|
|
178
|
-
? fileSendError
|
|
179
|
-
: new Error(String(fileSendError || "语音发送失败"));
|
|
185
|
+
throw new Error(`语音发送失败:${errorText(pathError)}`);
|
|
180
186
|
}
|
|
181
187
|
|
|
182
188
|
try {
|
|
183
189
|
await send(`base64://${buffer.toString("base64")}`);
|
|
184
190
|
} catch (base64Error) {
|
|
185
|
-
|
|
186
|
-
const timeoutLike =
|
|
187
|
-
message.includes("timeout") ||
|
|
188
|
-
message.includes("超时") ||
|
|
189
|
-
message.includes("timed out") ||
|
|
190
|
-
message.includes("ETIMEDOUT");
|
|
191
|
-
if (timeoutLike) {
|
|
191
|
+
if (isTimeoutError(base64Error)) {
|
|
192
192
|
return;
|
|
193
193
|
}
|
|
194
|
-
throw new Error(
|
|
194
|
+
throw new Error(
|
|
195
|
+
`语音发送失败:路径发送失败(${errorText(pathError)});base64 发送失败(${errorText(base64Error)})`,
|
|
196
|
+
);
|
|
195
197
|
}
|
|
196
198
|
}
|
package/runtime-core/service.ts
CHANGED
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
import { renderMusicSearchListHtml } from "../render/search-list";
|
|
19
19
|
import { MusicSessionStore } from "./session-store";
|
|
20
20
|
import { notifyFallback } from "./fallback";
|
|
21
|
+
import { prepareTrackFile } from "./audio-file";
|
|
21
22
|
import {
|
|
22
23
|
sendImageMessage,
|
|
23
24
|
sendRecordMessage,
|
|
@@ -26,6 +27,7 @@ import {
|
|
|
26
27
|
} from "./message";
|
|
27
28
|
import type {
|
|
28
29
|
MusicBaseConfig,
|
|
30
|
+
MusicProvider,
|
|
29
31
|
MusicProviderName,
|
|
30
32
|
MusicSearchResult,
|
|
31
33
|
MusicSessionState,
|
|
@@ -40,6 +42,12 @@ interface MusicPluginRuntimeDeps {
|
|
|
40
42
|
ncmdumpService?: NcmdumpServiceApi;
|
|
41
43
|
}
|
|
42
44
|
|
|
45
|
+
interface SendSongOptions {
|
|
46
|
+
title?: string;
|
|
47
|
+
artist?: string;
|
|
48
|
+
forceFile?: boolean;
|
|
49
|
+
}
|
|
50
|
+
|
|
43
51
|
export class MusicPluginRuntime {
|
|
44
52
|
private readonly deps: MusicPluginRuntimeDeps;
|
|
45
53
|
private readonly sessions = new MusicSessionStore();
|
|
@@ -147,8 +155,7 @@ export class MusicPluginRuntime {
|
|
|
147
155
|
ctx: any,
|
|
148
156
|
event: any,
|
|
149
157
|
songId: string,
|
|
150
|
-
|
|
151
|
-
options?: { forceFile?: boolean },
|
|
158
|
+
options?: SendSongOptions,
|
|
152
159
|
): Promise<void> {
|
|
153
160
|
const session = this.getOrCreateSession(event);
|
|
154
161
|
const provider = await this.createProvider(
|
|
@@ -170,11 +177,59 @@ export class MusicPluginRuntime {
|
|
|
170
177
|
}
|
|
171
178
|
const isPrivate = event?.message_type !== "group";
|
|
172
179
|
const sendAsFile = isPrivate || options?.forceFile === true;
|
|
173
|
-
if (sendAsFile) {
|
|
174
|
-
const finalName = fileName || "song";
|
|
175
|
-
await sendFileMessage(ctx, event, result.filePath, finalName);
|
|
176
|
-
} else {
|
|
180
|
+
if (!sendAsFile) {
|
|
177
181
|
await sendRecordMessage(ctx, event, result.filePath);
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const track = await this.resolveTrackMeta(
|
|
186
|
+
provider,
|
|
187
|
+
songId,
|
|
188
|
+
options,
|
|
189
|
+
session.lastSearch,
|
|
190
|
+
);
|
|
191
|
+
const prepared = await prepareTrackFile({
|
|
192
|
+
sourcePath: result.filePath,
|
|
193
|
+
title: track.title,
|
|
194
|
+
artist: track.artist,
|
|
195
|
+
fallbackName: `song-${songId}`,
|
|
196
|
+
});
|
|
197
|
+
try {
|
|
198
|
+
await sendFileMessage(ctx, event, prepared.filePath, prepared.fileName);
|
|
199
|
+
} finally {
|
|
200
|
+
await prepared.cleanup();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private async resolveTrackMeta(
|
|
205
|
+
provider: MusicProvider,
|
|
206
|
+
songId: string,
|
|
207
|
+
options?: SendSongOptions,
|
|
208
|
+
lastSearch?: MusicSearchResult,
|
|
209
|
+
): Promise<{ title: string; artist: string }> {
|
|
210
|
+
const title = String(options?.title ?? "").trim();
|
|
211
|
+
const artist = String(options?.artist ?? "").trim();
|
|
212
|
+
if (title) {
|
|
213
|
+
return { title, artist };
|
|
214
|
+
}
|
|
215
|
+
const known = lastSearch?.tracks.find((item) => item.id === songId);
|
|
216
|
+
if (known) {
|
|
217
|
+
return {
|
|
218
|
+
title: String(known.title ?? "").trim(),
|
|
219
|
+
artist: String(known.artist ?? "").trim(),
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
try {
|
|
223
|
+
const detail = await provider.getSongDetail(songId);
|
|
224
|
+
return {
|
|
225
|
+
title: String(detail.title ?? "").trim(),
|
|
226
|
+
artist: String(detail.artist ?? "").trim(),
|
|
227
|
+
};
|
|
228
|
+
} catch (error) {
|
|
229
|
+
this.deps.logger.warn(
|
|
230
|
+
`[music] 获取歌曲信息失败 songId=${songId}: ${String(error)}`,
|
|
231
|
+
);
|
|
232
|
+
return { title: "", artist: "" };
|
|
178
233
|
}
|
|
179
234
|
}
|
|
180
235
|
|
|
@@ -212,7 +267,11 @@ export class MusicPluginRuntime {
|
|
|
212
267
|
return;
|
|
213
268
|
}
|
|
214
269
|
|
|
215
|
-
await this.sendSongById(ctx, event, first.id,
|
|
270
|
+
await this.sendSongById(ctx, event, first.id, {
|
|
271
|
+
title: first.title,
|
|
272
|
+
artist: first.artist,
|
|
273
|
+
forceFile: options?.forceFile,
|
|
274
|
+
});
|
|
216
275
|
}
|
|
217
276
|
|
|
218
277
|
async searchAndSendList(
|
|
@@ -287,7 +346,9 @@ export class MusicPluginRuntime {
|
|
|
287
346
|
}
|
|
288
347
|
|
|
289
348
|
try {
|
|
290
|
-
await this.sendSongById(ctx, event, target.id,
|
|
349
|
+
await this.sendSongById(ctx, event, target.id, {
|
|
350
|
+
title: target.title,
|
|
351
|
+
artist: target.artist,
|
|
291
352
|
forceFile,
|
|
292
353
|
});
|
|
293
354
|
} catch (error) {
|
package/skills/music.ts
CHANGED
|
@@ -101,7 +101,7 @@ export function createMusicSkills(runtime: MusicPluginRuntime): AISkill[] {
|
|
|
101
101
|
|
|
102
102
|
try {
|
|
103
103
|
if (songId) {
|
|
104
|
-
await runtime.sendSongById(ctx, event, songId,
|
|
104
|
+
await runtime.sendSongById(ctx, event, songId, {
|
|
105
105
|
forceFile: asFile,
|
|
106
106
|
});
|
|
107
107
|
return asFile ? "已发送歌曲源文件" : "已发送歌曲";
|