@doitian/dsh-music 0.1.9 → 1.0.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.
- package/README.md +9 -2
- package/lib/client.js +13 -5
- package/lib/dj.js +135 -3
- package/lib/index.js +12 -1
- package/lib/netease.js +98 -0
- package/lib/panel.html +475 -83
- package/lib/router.js +50 -4
- package/lib/session.js +21 -0
- package/lib/state.js +34 -3
- package/package.json +1 -1
package/lib/router.js
CHANGED
|
@@ -21,12 +21,19 @@
|
|
|
21
21
|
|
|
22
22
|
import { Readable } from 'node:stream';
|
|
23
23
|
|
|
24
|
+
import { TtlCache } from './cache.js';
|
|
24
25
|
import { DEFAULT_LEVEL, LEVELS, isLevel } from './netease.js';
|
|
25
26
|
import { BOOST_DIRECTIONS, TASTE_LEVELS } from './session.js';
|
|
26
27
|
|
|
27
28
|
/** Resolved CDN URLs are reused for this long (they live 20 minutes). */
|
|
28
29
|
const URL_CACHE_MS = 15 * 60 * 1000;
|
|
29
30
|
|
|
31
|
+
/** Song details are kept this long: a wiki or an introduction is edited rarely. */
|
|
32
|
+
const INFO_TTL_MS = 6 * 60 * 60 * 1000;
|
|
33
|
+
|
|
34
|
+
/** A written introduction is kept longer: each one costs a model call. */
|
|
35
|
+
const INTRO_TTL_MS = 7 * 24 * 60 * 60 * 1000;
|
|
36
|
+
|
|
30
37
|
/**
|
|
31
38
|
* Version of the page/engine boundary, advertised on `/health`.
|
|
32
39
|
*
|
|
@@ -129,6 +136,15 @@ export class MusicRouter {
|
|
|
129
136
|
this.pendingUrls = new Map();
|
|
130
137
|
/** What the last stream request actually resolved, for the UI and health. */
|
|
131
138
|
this.lastAudio = null;
|
|
139
|
+
/** Song details for the panel, which barely change; each costs up to six requests. */
|
|
140
|
+
this.infoCache = new TtlCache({ maxEntries: 200 });
|
|
141
|
+
/** Model-written introductions, per track and language. */
|
|
142
|
+
this.introCache = new TtlCache({ maxEntries: 400 });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A song's details, shared by the details pane and the introduction written from them. */
|
|
146
|
+
songInfo(id) {
|
|
147
|
+
return this.infoCache.get(`info:${id}`, INFO_TTL_MS, () => this.api.songInfo(id));
|
|
132
148
|
}
|
|
133
149
|
|
|
134
150
|
/**
|
|
@@ -527,14 +543,18 @@ export class MusicRouter {
|
|
|
527
543
|
return sendJson(res, 200, await this.snapshot());
|
|
528
544
|
}
|
|
529
545
|
if (route === '/queue' && method === 'POST') {
|
|
530
|
-
const { action, tracks, index } = await readJson(req);
|
|
546
|
+
const { action, tracks, index, keepCurrent } = await readJson(req);
|
|
531
547
|
let removal = null;
|
|
532
548
|
if (action === 'replace' && Array.isArray(tracks)) this.player.setQueue(tracks, { source: 'user' });
|
|
533
549
|
else if (action === 'append' && Array.isArray(tracks)) this.player.append(tracks, { source: 'user' });
|
|
534
550
|
else if (action === 'insert' && Array.isArray(tracks)) this.player.insertNext(tracks, { source: 'user' });
|
|
535
551
|
else if (action === 'remove' || action === 'jump') removal = this.#queueAction(action, index);
|
|
536
|
-
else if (action === 'clear')
|
|
537
|
-
|
|
552
|
+
else if (action === 'clear') {
|
|
553
|
+
// Unlike `remove`, clearing is housekeeping, not a judgement: nothing is rated.
|
|
554
|
+
if (keepCurrent) this.player.clearKeepingCurrent();
|
|
555
|
+
else this.player.clear();
|
|
556
|
+
this.player.requestExtension();
|
|
557
|
+
} else return sendJson(res, 400, { error: `unknown queue action "${action}"` });
|
|
538
558
|
// Answer with the queue as it now stands, but record the judgement first:
|
|
539
559
|
// the snapshot's hearts should already show it.
|
|
540
560
|
if (removal?.dropped && removal.wasCurrent) await this.removeFromQueue(removal.track, true);
|
|
@@ -561,6 +581,32 @@ export class MusicRouter {
|
|
|
561
581
|
const id = Number(route.slice('/lyric/'.length));
|
|
562
582
|
return sendJson(res, 200, await this.api.lyric(id));
|
|
563
583
|
}
|
|
584
|
+
if (route.startsWith('/info/') && method === 'GET') {
|
|
585
|
+
const id = Number(route.slice('/info/'.length));
|
|
586
|
+
if (!Number.isInteger(id) || id <= 0) return sendJson(res, 400, { error: 'invalid track id' });
|
|
587
|
+
return sendJson(res, 200, await this.songInfo(id));
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* A model-written introduction to the song. Always 200: with no model, or a
|
|
591
|
+
* failed one, `text` is empty and the panel shows NetEase's own prose. Only
|
|
592
|
+
* a written one is cached, so a model set up later is used at once.
|
|
593
|
+
*/
|
|
594
|
+
if (route.startsWith('/intro/') && method === 'GET') {
|
|
595
|
+
const id = Number(route.slice('/intro/'.length));
|
|
596
|
+
if (!Number.isInteger(id) || id <= 0) return sendJson(res, 400, { error: 'invalid track id' });
|
|
597
|
+
const lang = query.get('lang') === 'zh' ? 'zh' : 'en';
|
|
598
|
+
try {
|
|
599
|
+
const intro = await this.introCache.get(`intro:${id}:${lang}`, INTRO_TTL_MS, async () => {
|
|
600
|
+
const written = await this.dj.introduce(await this.songInfo(id), { lang });
|
|
601
|
+
if (!written) throw new Error('no model route is available');
|
|
602
|
+
return written;
|
|
603
|
+
});
|
|
604
|
+
return sendJson(res, 200, intro);
|
|
605
|
+
} catch (error) {
|
|
606
|
+
this.logger?.debug?.(`[music] no introduction for ${id}: ${error.message}`);
|
|
607
|
+
return sendJson(res, 200, { text: '', reason: error.message });
|
|
608
|
+
}
|
|
609
|
+
}
|
|
564
610
|
if (route === '/charts' && method === 'GET') {
|
|
565
611
|
return sendJson(res, 200, await this.api.toplists());
|
|
566
612
|
}
|
|
@@ -707,7 +753,7 @@ export class MusicRouter {
|
|
|
707
753
|
`[music] DJ model route set to ${this.dj.model.provider ?? '(none)'}/${this.dj.model.model ?? '(none)'} from the player`,
|
|
708
754
|
);
|
|
709
755
|
}
|
|
710
|
-
if (prompt !== undefined) this.
|
|
756
|
+
if (prompt !== undefined) this.dj.setPrompt(prompt);
|
|
711
757
|
if (enabled !== undefined) this.dj.setEnabled(enabled);
|
|
712
758
|
if (replan || (enabled && this.player.queue.length === 0)) {
|
|
713
759
|
// Planning is slow; answer immediately and let the panel poll the result.
|
package/lib/session.js
CHANGED
|
@@ -40,6 +40,9 @@ const BOOST_MAX_MINUTES = 12 * 60;
|
|
|
40
40
|
/** Boosts held at once; the oldest goes first. */
|
|
41
41
|
const MAX_BOOSTS = 10;
|
|
42
42
|
|
|
43
|
+
/** Queue entries kept across a restart; the oldest played ones go first. */
|
|
44
|
+
const MAX_SAVED_QUEUE = 500;
|
|
45
|
+
|
|
43
46
|
/** QR codes are refreshed before the API reports them expired. */
|
|
44
47
|
const QR_TTL_MS = 110_000;
|
|
45
48
|
|
|
@@ -94,6 +97,11 @@ function emptyState() {
|
|
|
94
97
|
* passing mood does not become a standing preference.
|
|
95
98
|
*/
|
|
96
99
|
boosts: [],
|
|
100
|
+
/**
|
|
101
|
+
* The player's queue and cursor, so a restart picks up where it left off
|
|
102
|
+
* instead of waiting on the DJ's first plan.
|
|
103
|
+
*/
|
|
104
|
+
queue: { tracks: [], index: -1 },
|
|
97
105
|
};
|
|
98
106
|
}
|
|
99
107
|
|
|
@@ -205,6 +213,19 @@ export class SessionStore {
|
|
|
205
213
|
return this.state.settings;
|
|
206
214
|
}
|
|
207
215
|
|
|
216
|
+
// ----------------------------------------------------------------- queue
|
|
217
|
+
|
|
218
|
+
get savedQueue() {
|
|
219
|
+
return this.state.queue ?? { tracks: [], index: -1 };
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/** Keep the queue for the next run, dropping the oldest played tracks past the bound. */
|
|
223
|
+
saveQueue(tracks, index) {
|
|
224
|
+
const drop = Math.max(Math.min(tracks.length - MAX_SAVED_QUEUE, index), 0);
|
|
225
|
+
this.state.queue = { tracks: tracks.slice(drop, drop + MAX_SAVED_QUEUE), index: index - drop };
|
|
226
|
+
this.touch();
|
|
227
|
+
}
|
|
228
|
+
|
|
208
229
|
// --------------------------------------------------------------- history
|
|
209
230
|
|
|
210
231
|
/**
|
package/lib/state.js
CHANGED
|
@@ -61,8 +61,11 @@ export class Player {
|
|
|
61
61
|
this.muted = false;
|
|
62
62
|
/** One-shot absolute seek target in milliseconds. */
|
|
63
63
|
this.pendingSeek = null;
|
|
64
|
-
/**
|
|
65
|
-
|
|
64
|
+
/**
|
|
65
|
+
* Last state the panel reported back. `blocked` is whether the browser
|
|
66
|
+
* refused the last attempt to start audio for want of a user gesture.
|
|
67
|
+
*/
|
|
68
|
+
this.reported = { trackId: null, position: 0, duration: 0, playing: false, blocked: false, at: 0, error: null };
|
|
66
69
|
/**
|
|
67
70
|
* Last playback failure, preserved across the automatic skip that follows
|
|
68
71
|
* it. `reported.error` is per-track and cleared on every move, so without
|
|
@@ -249,6 +252,31 @@ export class Player {
|
|
|
249
252
|
return this.snapshot();
|
|
250
253
|
}
|
|
251
254
|
|
|
255
|
+
/**
|
|
256
|
+
* Empty the queue but for the track at the cursor, which plays on
|
|
257
|
+
* undisturbed: its play stays open, so it is neither ended nor restarted.
|
|
258
|
+
*/
|
|
259
|
+
clearKeepingCurrent() {
|
|
260
|
+
const current = this.current();
|
|
261
|
+
if (!current) return this.clear();
|
|
262
|
+
this.queue = [current];
|
|
263
|
+
this.index = 0;
|
|
264
|
+
this.bump({ transport: true });
|
|
265
|
+
return this.snapshot();
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Put back a queue saved by an earlier run, paused at its cursor. Nothing is
|
|
270
|
+
* finished or bumped: no play was open, and nobody is subscribed yet.
|
|
271
|
+
*/
|
|
272
|
+
restore({ tracks, index } = {}) {
|
|
273
|
+
const queue = Array.isArray(tracks) ? tracks.filter((track) => track?.id) : [];
|
|
274
|
+
if (queue.length === 0) return;
|
|
275
|
+
this.queue = queue;
|
|
276
|
+
this.index = Math.min(Math.max(Number(index) || 0, 0), queue.length - 1);
|
|
277
|
+
this.playing = false;
|
|
278
|
+
}
|
|
279
|
+
|
|
252
280
|
// --------------------------------------------------------------- transport
|
|
253
281
|
|
|
254
282
|
/** Index to advance to, honouring the repeat/shuffle mode. */
|
|
@@ -439,7 +467,7 @@ export class Player {
|
|
|
439
467
|
* did in response, so the caller can run follow-up work such as a DJ top-up.
|
|
440
468
|
* @returns {{ended: boolean, failed: boolean, advanced: boolean}}
|
|
441
469
|
*/
|
|
442
|
-
report({ trackId, position, duration, playing, ended, error } = {}) {
|
|
470
|
+
report({ trackId, position, duration, playing, blocked, ended, error } = {}) {
|
|
443
471
|
const current = this.current();
|
|
444
472
|
// Ignore reports for a track the user already moved away from; they arrive
|
|
445
473
|
// for a moment after every skip.
|
|
@@ -449,6 +477,9 @@ export class Player {
|
|
|
449
477
|
if (Number.isFinite(position)) this.reported.position = Number(position);
|
|
450
478
|
if (Number.isFinite(duration) && duration > 0) this.reported.duration = Number(duration);
|
|
451
479
|
if (typeof playing === 'boolean') this.reported.playing = playing;
|
|
480
|
+
// Audio that plays is the end of a block, whichever report says so.
|
|
481
|
+
if (playing === true) this.reported.blocked = false;
|
|
482
|
+
else if (typeof blocked === 'boolean') this.reported.blocked = blocked;
|
|
452
483
|
this.reported.trackId = current?.id ?? null;
|
|
453
484
|
this.reported.at = Date.now();
|
|
454
485
|
if (error) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doitian/dsh-music",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "NetEase Cloud Music (music.163.com) player and AI DJ for DeepSeek Harness: a sidebar page with in-app playback, QR sign-in, and agent tools that curate the queue.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"dsh",
|