mino-voice-notes 0.1.0 → 0.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/LICENSE +21 -0
- package/README.md +2 -2
- package/lib/client.js +494 -0
- package/lib/client.js.map +1 -0
- package/lib/index.js +636 -0
- package/lib/types/client/VNotesPage.d.ts +7 -0
- package/lib/types/client/api.d.ts +21 -0
- package/lib/types/client/index.d.ts +9 -0
- package/lib/types/client/locales.d.ts +27 -0
- package/lib/types/client/recorder.d.ts +13 -0
- package/lib/types/index.d.ts +13 -0
- package/lib/types/store.d.ts +30 -0
- package/package.json +5 -5
- package/src/client/index.tsx +3 -3
- package/src/client/vnotes.module.css +2 -2
- package/src/index.ts +1 -1
- package/src/store.ts +1 -1
package/lib/index.js
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
// src/store.ts
|
|
5
|
+
/** mino-voice-notes 自有存储:notes.json 元数据索引 + audio/<id>/<seq>.wav 分段文件。
|
|
6
|
+
* 位置 `$DSH_HOME/mino-voice-notes/`(无 DSH_HOME 时 `~/.mino/mino-voice-notes/`)——
|
|
7
|
+
* 与 mino-lore 同一落点约定,不读其他插件私有目录。
|
|
8
|
+
*/
|
|
9
|
+
const ROOT = join(process.env.DSH_HOME || join(homedir(), ".mino"), "mino-voice-notes");
|
|
10
|
+
const AUDIO_DIR = join(ROOT, "audio");
|
|
11
|
+
const STORE_FILE = join(ROOT, "notes.json");
|
|
12
|
+
function ensureDirs() {
|
|
13
|
+
mkdirSync(AUDIO_DIR, { recursive: true });
|
|
14
|
+
}
|
|
15
|
+
function loadNotes() {
|
|
16
|
+
try {
|
|
17
|
+
const raw = readFileSync(STORE_FILE, "utf-8");
|
|
18
|
+
const parsed = JSON.parse(raw);
|
|
19
|
+
return parsed && typeof parsed === "object" && parsed.notes && typeof parsed.notes === "object" ? parsed.notes : {};
|
|
20
|
+
} catch {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function saveNotes(notes) {
|
|
25
|
+
ensureDirs();
|
|
26
|
+
writeFileSync(STORE_FILE, JSON.stringify({ notes }, null, 2));
|
|
27
|
+
}
|
|
28
|
+
function getNote(id) {
|
|
29
|
+
return loadNotes()[id];
|
|
30
|
+
}
|
|
31
|
+
function putNote(note) {
|
|
32
|
+
const notes = loadNotes();
|
|
33
|
+
notes[note.id] = note;
|
|
34
|
+
saveNotes(notes);
|
|
35
|
+
}
|
|
36
|
+
function deleteNote(id) {
|
|
37
|
+
const notes = loadNotes();
|
|
38
|
+
if (!notes[id]) return false;
|
|
39
|
+
delete notes[id];
|
|
40
|
+
saveNotes(notes);
|
|
41
|
+
try {
|
|
42
|
+
rmSync(join(AUDIO_DIR, id), {
|
|
43
|
+
recursive: true,
|
|
44
|
+
force: true
|
|
45
|
+
});
|
|
46
|
+
} catch {}
|
|
47
|
+
return true;
|
|
48
|
+
}
|
|
49
|
+
function segmentPath(id, seq) {
|
|
50
|
+
return join(AUDIO_DIR, id, `seg-${String(seq).padStart(4, "0")}.wav`);
|
|
51
|
+
}
|
|
52
|
+
function appendSegment(id, seq, wav, durationMs) {
|
|
53
|
+
const dir = join(AUDIO_DIR, id);
|
|
54
|
+
mkdirSync(dir, { recursive: true });
|
|
55
|
+
const file = segmentPath(id, seq);
|
|
56
|
+
writeFileSync(file, wav);
|
|
57
|
+
return {
|
|
58
|
+
seq,
|
|
59
|
+
file,
|
|
60
|
+
bytes: wav.length,
|
|
61
|
+
...durationMs !== void 0 ? { durationMs } : {}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function readSegment(id, seq) {
|
|
65
|
+
try {
|
|
66
|
+
return readFileSync(segmentPath(id, seq));
|
|
67
|
+
} catch {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function newId() {
|
|
72
|
+
return `vn_${Date.now().toString(36)}${Math.random().toString(36).slice(2, 8)}`;
|
|
73
|
+
}
|
|
74
|
+
// src/index.ts
|
|
75
|
+
const name = "mino-voice-notes";
|
|
76
|
+
const inject = ["webServer", "tools"];
|
|
77
|
+
const JSON_HEADERS = {
|
|
78
|
+
"content-type": "application/json; charset=utf-8",
|
|
79
|
+
"cache-control": "no-store"
|
|
80
|
+
};
|
|
81
|
+
const MAX_TITLE = 120;
|
|
82
|
+
const MAX_SEGMENTS = 400;
|
|
83
|
+
const MAX_SEGMENT_BYTES = 25165824;
|
|
84
|
+
const TRANSCRIBE_TIMEOUT_MS = 12e4;
|
|
85
|
+
function sendJson(res, status, body) {
|
|
86
|
+
res.writeHead(status, JSON_HEADERS);
|
|
87
|
+
res.end(JSON.stringify(body));
|
|
88
|
+
}
|
|
89
|
+
async function readBody(req) {
|
|
90
|
+
let raw = "";
|
|
91
|
+
for await (const chunk of req) raw += String(chunk);
|
|
92
|
+
if (!raw) return {};
|
|
93
|
+
try {
|
|
94
|
+
const parsed = JSON.parse(raw);
|
|
95
|
+
return parsed && typeof parsed === "object" ? parsed : {};
|
|
96
|
+
} catch {
|
|
97
|
+
return {};
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const clip = (s, max) => {
|
|
101
|
+
if (s === void 0 || s === null) return void 0;
|
|
102
|
+
return String(s).slice(0, max) || void 0;
|
|
103
|
+
};
|
|
104
|
+
const esc = (s) => s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
105
|
+
function notePublic(n) {
|
|
106
|
+
return {
|
|
107
|
+
id: n.id,
|
|
108
|
+
title: n.title,
|
|
109
|
+
status: n.status,
|
|
110
|
+
createdAt: n.createdAt,
|
|
111
|
+
stoppedAt: n.stoppedAt,
|
|
112
|
+
segments: n.segments.length,
|
|
113
|
+
transcript: n.transcript,
|
|
114
|
+
summary: n.summary,
|
|
115
|
+
shareId: n.shareId,
|
|
116
|
+
error: n.error,
|
|
117
|
+
audioSeconds: n.audioSeconds
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
function startNote(title) {
|
|
121
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
122
|
+
const note = {
|
|
123
|
+
id: newId(),
|
|
124
|
+
title: (title || `录音 ${now.slice(0, 16).replace("T", " ")}`).slice(0, MAX_TITLE),
|
|
125
|
+
status: "recording",
|
|
126
|
+
createdAt: now,
|
|
127
|
+
updatedAt: now,
|
|
128
|
+
segments: []
|
|
129
|
+
};
|
|
130
|
+
putNote(note);
|
|
131
|
+
return note;
|
|
132
|
+
}
|
|
133
|
+
async function transcribeNote(ctx, note) {
|
|
134
|
+
const stt = ctx.get("speechToText");
|
|
135
|
+
if (!stt?.transcribe) {
|
|
136
|
+
note.status = "failed";
|
|
137
|
+
note.error = "没有可用的语音转写服务(装 voice-input bundle / 配 ASR provider)";
|
|
138
|
+
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
139
|
+
putNote(note);
|
|
140
|
+
return note;
|
|
141
|
+
}
|
|
142
|
+
const segments = [...note.segments].sort((a, b) => a.seq - b.seq);
|
|
143
|
+
if (!segments.length) {
|
|
144
|
+
note.status = "failed";
|
|
145
|
+
note.error = "没有音频分段";
|
|
146
|
+
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
147
|
+
putNote(note);
|
|
148
|
+
return note;
|
|
149
|
+
}
|
|
150
|
+
const texts = [];
|
|
151
|
+
let audioSeconds = 0;
|
|
152
|
+
const controller = new AbortController();
|
|
153
|
+
const timer = setTimeout(() => controller.abort(/* @__PURE__ */ new Error("transcribe timeout")), TRANSCRIBE_TIMEOUT_MS);
|
|
154
|
+
try {
|
|
155
|
+
for (const seg of segments) {
|
|
156
|
+
const wav = readSegment(note.id, seg.seq);
|
|
157
|
+
if (!wav) continue;
|
|
158
|
+
const spec = stt.resolve({ audio: new Uint8Array(wav) });
|
|
159
|
+
const out = await stt.transcribe(spec, controller.signal);
|
|
160
|
+
if (out.text?.trim()) texts.push(out.text.trim());
|
|
161
|
+
audioSeconds += out.audioSeconds || 0;
|
|
162
|
+
}
|
|
163
|
+
note.transcript = texts.join("\n");
|
|
164
|
+
note.audioSeconds = audioSeconds || void 0;
|
|
165
|
+
note.status = "ready";
|
|
166
|
+
note.error = void 0;
|
|
167
|
+
} catch (error) {
|
|
168
|
+
note.status = "failed";
|
|
169
|
+
note.error = `转写失败:${String(error?.message ?? error)}`;
|
|
170
|
+
} finally {
|
|
171
|
+
clearTimeout(timer);
|
|
172
|
+
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
173
|
+
putNote(note);
|
|
174
|
+
}
|
|
175
|
+
return note;
|
|
176
|
+
}
|
|
177
|
+
function stopAndTranscribe(ctx, id) {
|
|
178
|
+
const note = getNote(id);
|
|
179
|
+
if (!note) return "not_found";
|
|
180
|
+
if (note.status !== "recording") return "bad_state";
|
|
181
|
+
note.status = "transcribing";
|
|
182
|
+
note.stoppedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
183
|
+
note.updatedAt = note.stoppedAt;
|
|
184
|
+
putNote(note);
|
|
185
|
+
transcribeNote(ctx, note).catch(() => {});
|
|
186
|
+
return note;
|
|
187
|
+
}
|
|
188
|
+
const PARAM_ID = {
|
|
189
|
+
type: "object",
|
|
190
|
+
additionalProperties: false,
|
|
191
|
+
required: ["id"],
|
|
192
|
+
properties: { id: {
|
|
193
|
+
type: "string",
|
|
194
|
+
description: "笔记 id(vn_ 开头)。"
|
|
195
|
+
} }
|
|
196
|
+
};
|
|
197
|
+
const OUT_NOTE = {
|
|
198
|
+
type: "object",
|
|
199
|
+
additionalProperties: false,
|
|
200
|
+
properties: {
|
|
201
|
+
id: { type: "string" },
|
|
202
|
+
title: { type: "string" },
|
|
203
|
+
status: { type: "string" },
|
|
204
|
+
transcript: { type: "string" },
|
|
205
|
+
summary: { type: "string" },
|
|
206
|
+
error: { type: "string" }
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
function makeTools(ctx) {
|
|
210
|
+
return [
|
|
211
|
+
{
|
|
212
|
+
name: "vnote_start",
|
|
213
|
+
description: "开始一段语音笔记录音。返回笔记 id——客户端的「语音笔记」面板会用这个 id 上传音频分段;录完用 vnote_stop 收尾并转写。",
|
|
214
|
+
parameters: {
|
|
215
|
+
type: "object",
|
|
216
|
+
additionalProperties: false,
|
|
217
|
+
properties: { title: {
|
|
218
|
+
type: "string",
|
|
219
|
+
description: "可选。给这段录音起个名(≤120字)。"
|
|
220
|
+
} }
|
|
221
|
+
},
|
|
222
|
+
output: {
|
|
223
|
+
schema: OUT_NOTE,
|
|
224
|
+
render: (_a, v) => [{
|
|
225
|
+
type: "text",
|
|
226
|
+
text: `开始录音 ${v?.id ?? ""}`
|
|
227
|
+
}]
|
|
228
|
+
},
|
|
229
|
+
isConcurrencySafe: () => false,
|
|
230
|
+
async execute(args) {
|
|
231
|
+
const note = startNote(args.title);
|
|
232
|
+
return {
|
|
233
|
+
id: note.id,
|
|
234
|
+
title: note.title,
|
|
235
|
+
status: note.status
|
|
236
|
+
};
|
|
237
|
+
},
|
|
238
|
+
presentCall: (a) => ({
|
|
239
|
+
card: "generic",
|
|
240
|
+
title: a?.title ? `开始录音:${a.title}` : "开始录音",
|
|
241
|
+
kind: "other",
|
|
242
|
+
rawInput: a
|
|
243
|
+
})
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
name: "vnote_stop",
|
|
247
|
+
description: "结束一段录音并触发本机转写。转写在后台跑——长录音别干等,过几秒用 vnote_get 取结果。",
|
|
248
|
+
parameters: PARAM_ID,
|
|
249
|
+
output: {
|
|
250
|
+
schema: OUT_NOTE,
|
|
251
|
+
render: (_a, v) => [{
|
|
252
|
+
type: "text",
|
|
253
|
+
text: `收尾:${v?.status ?? ""}`
|
|
254
|
+
}]
|
|
255
|
+
},
|
|
256
|
+
isConcurrencySafe: () => false,
|
|
257
|
+
async execute(args) {
|
|
258
|
+
const r = stopAndTranscribe(ctx, String(args.id || ""));
|
|
259
|
+
if (r === "not_found") throw new Error(`找不到笔记 ${args.id}`);
|
|
260
|
+
if (r === "bad_state") throw new Error("这段录音不在录制中(可能已收尾)");
|
|
261
|
+
return {
|
|
262
|
+
id: r.id,
|
|
263
|
+
title: r.title,
|
|
264
|
+
status: r.status
|
|
265
|
+
};
|
|
266
|
+
},
|
|
267
|
+
presentCall: () => ({
|
|
268
|
+
card: "generic",
|
|
269
|
+
title: "结束录音",
|
|
270
|
+
kind: "other"
|
|
271
|
+
})
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
name: "vnote_list",
|
|
275
|
+
description: "列出最近的语音笔记(id/标题/状态/时长),不含转写全文——要全文用 vnote_get。",
|
|
276
|
+
parameters: {
|
|
277
|
+
type: "object",
|
|
278
|
+
additionalProperties: false,
|
|
279
|
+
properties: { limit: {
|
|
280
|
+
type: "integer",
|
|
281
|
+
description: "最近多少条(默认 20)"
|
|
282
|
+
} }
|
|
283
|
+
},
|
|
284
|
+
output: {
|
|
285
|
+
schema: {
|
|
286
|
+
type: "object",
|
|
287
|
+
additionalProperties: false,
|
|
288
|
+
properties: {
|
|
289
|
+
count: { type: "integer" },
|
|
290
|
+
notes: {
|
|
291
|
+
type: "array",
|
|
292
|
+
items: {
|
|
293
|
+
type: "object",
|
|
294
|
+
additionalProperties: true
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
render: (_a, v) => [{
|
|
300
|
+
type: "text",
|
|
301
|
+
text: `${v?.count ?? 0} 条语音笔记`
|
|
302
|
+
}]
|
|
303
|
+
},
|
|
304
|
+
isConcurrencySafe: () => true,
|
|
305
|
+
async execute(args) {
|
|
306
|
+
const limit = Math.min(Math.max(Number(args.limit) || 20, 1), 100);
|
|
307
|
+
const notes = Object.values(loadNotes()).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, limit).map(notePublic);
|
|
308
|
+
return {
|
|
309
|
+
count: notes.length,
|
|
310
|
+
notes
|
|
311
|
+
};
|
|
312
|
+
},
|
|
313
|
+
presentCall: () => ({
|
|
314
|
+
card: "generic",
|
|
315
|
+
title: "看语音笔记",
|
|
316
|
+
kind: "other"
|
|
317
|
+
})
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
name: "vnote_get",
|
|
321
|
+
description: "取一条语音笔记的完整信息:转写全文、纪要、状态。转写文可能很长。",
|
|
322
|
+
parameters: PARAM_ID,
|
|
323
|
+
output: {
|
|
324
|
+
schema: OUT_NOTE,
|
|
325
|
+
render: (_a, v) => [{
|
|
326
|
+
type: "text",
|
|
327
|
+
text: `笔记 ${v?.status ?? ""}`
|
|
328
|
+
}]
|
|
329
|
+
},
|
|
330
|
+
isConcurrencySafe: () => true,
|
|
331
|
+
async execute(args) {
|
|
332
|
+
const note = getNote(String(args.id || ""));
|
|
333
|
+
if (!note) throw new Error(`找不到笔记 ${args.id}`);
|
|
334
|
+
return notePublic(note);
|
|
335
|
+
},
|
|
336
|
+
presentCall: () => ({
|
|
337
|
+
card: "generic",
|
|
338
|
+
title: "读语音笔记",
|
|
339
|
+
kind: "other"
|
|
340
|
+
})
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
name: "vnote_update",
|
|
344
|
+
description: "改一条语音笔记的标题或纪要。转写完之后,把读出来的要点写成 summary 存回去。",
|
|
345
|
+
parameters: {
|
|
346
|
+
type: "object",
|
|
347
|
+
additionalProperties: false,
|
|
348
|
+
required: ["id"],
|
|
349
|
+
properties: {
|
|
350
|
+
id: {
|
|
351
|
+
type: "string",
|
|
352
|
+
description: "笔记 id。"
|
|
353
|
+
},
|
|
354
|
+
title: {
|
|
355
|
+
type: "string",
|
|
356
|
+
description: "可选。新标题(≤120字)。"
|
|
357
|
+
},
|
|
358
|
+
summary: {
|
|
359
|
+
type: "string",
|
|
360
|
+
description: "可选。纪要文字(≤4000字)。"
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
},
|
|
364
|
+
output: {
|
|
365
|
+
schema: OUT_NOTE,
|
|
366
|
+
render: () => [{
|
|
367
|
+
type: "text",
|
|
368
|
+
text: "已更新"
|
|
369
|
+
}]
|
|
370
|
+
},
|
|
371
|
+
isConcurrencySafe: () => false,
|
|
372
|
+
async execute(args) {
|
|
373
|
+
const note = getNote(String(args.id || ""));
|
|
374
|
+
if (!note) throw new Error(`找不到笔记 ${args.id}`);
|
|
375
|
+
const title = clip(args.title, MAX_TITLE);
|
|
376
|
+
const summary = clip(args.summary, 4e3);
|
|
377
|
+
if (title !== void 0) note.title = title;
|
|
378
|
+
if (summary !== void 0) note.summary = summary;
|
|
379
|
+
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
380
|
+
putNote(note);
|
|
381
|
+
return {
|
|
382
|
+
id: note.id,
|
|
383
|
+
title: note.title,
|
|
384
|
+
status: note.status
|
|
385
|
+
};
|
|
386
|
+
},
|
|
387
|
+
presentCall: () => ({
|
|
388
|
+
card: "generic",
|
|
389
|
+
title: "改语音笔记",
|
|
390
|
+
kind: "other"
|
|
391
|
+
})
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
name: "vnote_share",
|
|
395
|
+
description: "给一条已完成的语音笔记生成本地只读分享页(转写+纪要+音频分段),返回页面路径。",
|
|
396
|
+
parameters: PARAM_ID,
|
|
397
|
+
output: {
|
|
398
|
+
schema: {
|
|
399
|
+
type: "object",
|
|
400
|
+
additionalProperties: false,
|
|
401
|
+
properties: {
|
|
402
|
+
shareId: { type: "string" },
|
|
403
|
+
path: { type: "string" }
|
|
404
|
+
}
|
|
405
|
+
},
|
|
406
|
+
render: (_a, v) => [{
|
|
407
|
+
type: "text",
|
|
408
|
+
text: `分享页 ${v?.path ?? ""}`
|
|
409
|
+
}]
|
|
410
|
+
},
|
|
411
|
+
isConcurrencySafe: () => false,
|
|
412
|
+
async execute(args) {
|
|
413
|
+
const note = getNote(String(args.id || ""));
|
|
414
|
+
if (!note) throw new Error(`找不到笔记 ${args.id}`);
|
|
415
|
+
if (!note.shareId) {
|
|
416
|
+
note.shareId = Math.random().toString(36).slice(2, 10) + Date.now().toString(36);
|
|
417
|
+
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
418
|
+
putNote(note);
|
|
419
|
+
}
|
|
420
|
+
return {
|
|
421
|
+
shareId: note.shareId,
|
|
422
|
+
path: `/mino-voice-notes/s/${note.shareId}`
|
|
423
|
+
};
|
|
424
|
+
},
|
|
425
|
+
presentCall: () => ({
|
|
426
|
+
card: "generic",
|
|
427
|
+
title: "分享语音笔记",
|
|
428
|
+
kind: "other"
|
|
429
|
+
})
|
|
430
|
+
}
|
|
431
|
+
];
|
|
432
|
+
}
|
|
433
|
+
function sharePage(note) {
|
|
434
|
+
const segs = note.segments.map((s) => `<li><a href="/mino-voice-notes/api/audio?id=${encodeURIComponent(note.id)}&seq=${s.seq}">分段 ${s.seq}</a>(${(s.bytes / 1024).toFixed(0)} KB)</li>`).join("");
|
|
435
|
+
return `<!doctype html><html lang="zh"><head><meta charset="utf-8"><title>${esc(note.title)}</title>
|
|
436
|
+
<meta name="viewport" content="width=device-width,initial-scale=1">
|
|
437
|
+
<style>body{font-family:system-ui,sans-serif;max-width:720px;margin:32px auto;padding:0 16px;color:#222}
|
|
438
|
+
h1{font-size:20px}h2{font-size:15px;margin-top:28px;color:#555}
|
|
439
|
+
pre{white-space:pre-wrap;background:#f6f6f6;padding:14px;border-radius:8px;font-size:14px;line-height:1.7}
|
|
440
|
+
ul{padding-left:20px}.muted{color:#888;font-size:12px}</style></head><body>
|
|
441
|
+
<h1>${esc(note.title)}</h1><div class="muted">${esc(note.createdAt)} · ${note.audioSeconds ? note.audioSeconds.toFixed(1) + "s" : ""}</div>
|
|
442
|
+
${note.summary ? `<h2>纪要</h2><pre>${esc(note.summary)}</pre>` : ""}
|
|
443
|
+
<h2>转写</h2><pre>${esc(note.transcript || "(无)")}</pre>
|
|
444
|
+
<h2>音频</h2><ul>${segs || "<li>无分段</li>"}</ul>
|
|
445
|
+
</body></html>`;
|
|
446
|
+
}
|
|
447
|
+
function apply(ctx) {
|
|
448
|
+
const webServer = ctx.get("webServer");
|
|
449
|
+
const tools = ctx.get("tools");
|
|
450
|
+
if (typeof ctx.provide === "function") ctx.provide("mino-voice-notes", {
|
|
451
|
+
start: (title) => startNote(title),
|
|
452
|
+
stop: (id) => {
|
|
453
|
+
const r = stopAndTranscribe(ctx, id);
|
|
454
|
+
return typeof r === "object" ? r : void 0;
|
|
455
|
+
},
|
|
456
|
+
status: (id) => getNote(id)
|
|
457
|
+
});
|
|
458
|
+
const route = (kind, path, handler) => {
|
|
459
|
+
if (!webServer) return;
|
|
460
|
+
ctx.effect(() => webServer.register({
|
|
461
|
+
kind,
|
|
462
|
+
path,
|
|
463
|
+
handler: async (req, res) => {
|
|
464
|
+
try {
|
|
465
|
+
await handler(req, res);
|
|
466
|
+
} catch (error) {
|
|
467
|
+
sendJson(res, 500, {
|
|
468
|
+
ok: false,
|
|
469
|
+
error: String(error?.message ?? error)
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
}));
|
|
474
|
+
};
|
|
475
|
+
route("exact", "/mino-voice-notes/api/start", async (req, res) => {
|
|
476
|
+
const body = await readBody(req);
|
|
477
|
+
const note = startNote(clip(body.title, MAX_TITLE));
|
|
478
|
+
sendJson(res, 200, {
|
|
479
|
+
ok: true,
|
|
480
|
+
id: note.id,
|
|
481
|
+
title: note.title
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
route("exact", "/mino-voice-notes/api/segment", async (req, res) => {
|
|
485
|
+
const body = await readBody(req);
|
|
486
|
+
const id = String(body.id || "");
|
|
487
|
+
const seq = Number(body.seq);
|
|
488
|
+
const note = getNote(id);
|
|
489
|
+
if (!note) return sendJson(res, 404, {
|
|
490
|
+
ok: false,
|
|
491
|
+
error: "找不到笔记"
|
|
492
|
+
});
|
|
493
|
+
if (note.status !== "recording") return sendJson(res, 409, {
|
|
494
|
+
ok: false,
|
|
495
|
+
error: "不在录制中"
|
|
496
|
+
});
|
|
497
|
+
if (!Number.isInteger(seq) || seq < 0 || seq >= MAX_SEGMENTS) return sendJson(res, 400, {
|
|
498
|
+
ok: false,
|
|
499
|
+
error: "seq 不对"
|
|
500
|
+
});
|
|
501
|
+
if (typeof body.audio !== "string" || !body.audio) return sendJson(res, 400, {
|
|
502
|
+
ok: false,
|
|
503
|
+
error: "audio 为空"
|
|
504
|
+
});
|
|
505
|
+
const wav = Buffer.from(body.audio, "base64");
|
|
506
|
+
if (!wav.length || wav.length > MAX_SEGMENT_BYTES) return sendJson(res, 400, {
|
|
507
|
+
ok: false,
|
|
508
|
+
error: "audio 大小不对"
|
|
509
|
+
});
|
|
510
|
+
if (wav.length > 12 && wav.toString("ascii", 0, 4) !== "RIFF") return sendJson(res, 400, {
|
|
511
|
+
ok: false,
|
|
512
|
+
error: "audio 不是 WAV"
|
|
513
|
+
});
|
|
514
|
+
const seg = appendSegment(id, seq, wav, Number.isFinite(+body.durationMs) ? +body.durationMs : void 0);
|
|
515
|
+
const fresh = getNote(id);
|
|
516
|
+
if (!fresh.segments.some((s) => s.seq === seq)) fresh.segments.push(seg);
|
|
517
|
+
else fresh.segments = fresh.segments.map((s) => s.seq === seq ? seg : s);
|
|
518
|
+
fresh.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
519
|
+
putNote(fresh);
|
|
520
|
+
sendJson(res, 200, {
|
|
521
|
+
ok: true,
|
|
522
|
+
seq,
|
|
523
|
+
bytes: seg.bytes
|
|
524
|
+
});
|
|
525
|
+
});
|
|
526
|
+
route("exact", "/mino-voice-notes/api/stop", async (req, res) => {
|
|
527
|
+
const body = await readBody(req);
|
|
528
|
+
const r = stopAndTranscribe(ctx, String(body.id || ""));
|
|
529
|
+
if (r === "not_found") return sendJson(res, 404, {
|
|
530
|
+
ok: false,
|
|
531
|
+
error: "找不到笔记"
|
|
532
|
+
});
|
|
533
|
+
if (r === "bad_state") return sendJson(res, 409, {
|
|
534
|
+
ok: false,
|
|
535
|
+
error: "不在录制中"
|
|
536
|
+
});
|
|
537
|
+
sendJson(res, 200, {
|
|
538
|
+
ok: true,
|
|
539
|
+
id: r.id,
|
|
540
|
+
status: r.status
|
|
541
|
+
});
|
|
542
|
+
});
|
|
543
|
+
route("exact", "/mino-voice-notes/api/list", async (_req, res) => {
|
|
544
|
+
sendJson(res, 200, {
|
|
545
|
+
ok: true,
|
|
546
|
+
notes: Object.values(loadNotes()).sort((a, b) => b.createdAt.localeCompare(a.createdAt)).slice(0, 100).map(notePublic)
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
route("exact", "/mino-voice-notes/api/note", async (req, res) => {
|
|
550
|
+
const note = getNote(new URL(req.url || "", "http://localhost").searchParams.get("id") || "");
|
|
551
|
+
if (!note) return sendJson(res, 404, {
|
|
552
|
+
ok: false,
|
|
553
|
+
error: "找不到笔记"
|
|
554
|
+
});
|
|
555
|
+
sendJson(res, 200, {
|
|
556
|
+
ok: true,
|
|
557
|
+
note: notePublic(note)
|
|
558
|
+
});
|
|
559
|
+
});
|
|
560
|
+
route("exact", "/mino-voice-notes/api/update", async (req, res) => {
|
|
561
|
+
const body = await readBody(req);
|
|
562
|
+
const note = getNote(String(body.id || ""));
|
|
563
|
+
if (!note) return sendJson(res, 404, {
|
|
564
|
+
ok: false,
|
|
565
|
+
error: "找不到笔记"
|
|
566
|
+
});
|
|
567
|
+
const title = clip(body.title, MAX_TITLE);
|
|
568
|
+
const summary = clip(body.summary, 4e3);
|
|
569
|
+
if (title !== void 0) note.title = title;
|
|
570
|
+
if (summary !== void 0) note.summary = summary;
|
|
571
|
+
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
572
|
+
putNote(note);
|
|
573
|
+
sendJson(res, 200, { ok: true });
|
|
574
|
+
});
|
|
575
|
+
route("exact", "/mino-voice-notes/api/share", async (req, res) => {
|
|
576
|
+
const body = await readBody(req);
|
|
577
|
+
const note = getNote(String(body.id || ""));
|
|
578
|
+
if (!note) return sendJson(res, 404, {
|
|
579
|
+
ok: false,
|
|
580
|
+
error: "找不到笔记"
|
|
581
|
+
});
|
|
582
|
+
if (!note.shareId) {
|
|
583
|
+
note.shareId = Math.random().toString(36).slice(2, 10) + Date.now().toString(36);
|
|
584
|
+
note.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
585
|
+
putNote(note);
|
|
586
|
+
}
|
|
587
|
+
sendJson(res, 200, {
|
|
588
|
+
ok: true,
|
|
589
|
+
shareId: note.shareId,
|
|
590
|
+
path: `/mino-voice-notes/s/${note.shareId}`
|
|
591
|
+
});
|
|
592
|
+
});
|
|
593
|
+
route("exact", "/mino-voice-notes/api/delete", async (req, res) => {
|
|
594
|
+
const body = await readBody(req);
|
|
595
|
+
if (!deleteNote(String(body.id || ""))) return sendJson(res, 404, {
|
|
596
|
+
ok: false,
|
|
597
|
+
error: "找不到笔记"
|
|
598
|
+
});
|
|
599
|
+
sendJson(res, 200, { ok: true });
|
|
600
|
+
});
|
|
601
|
+
route("exact", "/mino-voice-notes/api/audio", async (req, res) => {
|
|
602
|
+
const url = new URL(req.url || "", "http://localhost");
|
|
603
|
+
const note = getNote(url.searchParams.get("id") || "");
|
|
604
|
+
const seq = Number(url.searchParams.get("seq"));
|
|
605
|
+
if (!note || !Number.isInteger(seq)) return sendJson(res, 404, {
|
|
606
|
+
ok: false,
|
|
607
|
+
error: "找不到"
|
|
608
|
+
});
|
|
609
|
+
const wav = readSegment(note.id, seq);
|
|
610
|
+
if (!wav) return sendJson(res, 404, {
|
|
611
|
+
ok: false,
|
|
612
|
+
error: "找不到分段"
|
|
613
|
+
});
|
|
614
|
+
res.writeHead(200, {
|
|
615
|
+
"content-type": "audio/wav",
|
|
616
|
+
"content-length": String(wav.length),
|
|
617
|
+
"cache-control": "no-store"
|
|
618
|
+
});
|
|
619
|
+
res.end(wav.toString("binary"));
|
|
620
|
+
});
|
|
621
|
+
route("prefix", "/mino-voice-notes/s", async (req, res) => {
|
|
622
|
+
const shareId = (req.url || "").split("/mino-voice-notes/s/")[1]?.split(/[?#]/)[0] || "";
|
|
623
|
+
const note = Object.values(loadNotes()).find((n) => n.shareId === shareId);
|
|
624
|
+
if (!note) {
|
|
625
|
+
res.writeHead(404, { "content-type": "text/html; charset=utf-8" });
|
|
626
|
+
return res.end("<!doctype html><meta charset=\"utf-8\"><p>分享不存在或已删除。</p>");
|
|
627
|
+
}
|
|
628
|
+
res.writeHead(200, {
|
|
629
|
+
"content-type": "text/html; charset=utf-8",
|
|
630
|
+
"cache-control": "no-store"
|
|
631
|
+
});
|
|
632
|
+
res.end(sharePage(note));
|
|
633
|
+
});
|
|
634
|
+
if (tools) for (const tool of makeTools(ctx)) ctx.effect(() => tools.register(tool));
|
|
635
|
+
}
|
|
636
|
+
export { apply, inject, name };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** mino-voice-notes 客户端的 API 面:自有插件路由,同源 fetch 自动带会话鉴权。 */
|
|
2
|
+
export interface VNotePublic {
|
|
3
|
+
id: string;
|
|
4
|
+
title: string;
|
|
5
|
+
status: 'recording' | 'transcribing' | 'ready' | 'failed';
|
|
6
|
+
createdAt: string;
|
|
7
|
+
stoppedAt?: string;
|
|
8
|
+
segments: number;
|
|
9
|
+
transcript?: string;
|
|
10
|
+
summary?: string;
|
|
11
|
+
shareId?: string;
|
|
12
|
+
error?: string;
|
|
13
|
+
audioSeconds?: number;
|
|
14
|
+
}
|
|
15
|
+
export declare function listNotes(): Promise<VNotePublic[]>;
|
|
16
|
+
export declare const startNote: (title?: string) => Promise<Record<string, unknown>>;
|
|
17
|
+
export declare const stopNote: (id: string) => Promise<Record<string, unknown>>;
|
|
18
|
+
export declare const getNote: (id: string) => Promise<VNotePublic | undefined>;
|
|
19
|
+
export declare const shareNote: (id: string) => Promise<Record<string, unknown>>;
|
|
20
|
+
export declare const deleteNote: (id: string) => Promise<Record<string, unknown>>;
|
|
21
|
+
export declare const uploadSegment: (id: string, seq: number, wavBase64: string, durationMs: number) => Promise<Record<string, unknown>>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** mino-voice-notes 客户端半:注册进 mino-lore 的「语音」tab 座位。
|
|
2
|
+
* 与 mino-lore 同款注册法(slots.inject 座位缺席则安静缺席)。
|
|
3
|
+
* 数据面:同源 fetch 自有路由;录音经 MediaRecorder 同款 getUserMedia+PCM16 WAV 链。
|
|
4
|
+
*/
|
|
5
|
+
import type { Context } from '@deepseek-ai/cordis';
|
|
6
|
+
import { type CopyKey } from './locales.ts';
|
|
7
|
+
export declare const inject: string[];
|
|
8
|
+
export type Translate = (key: CopyKey, params?: Record<string, unknown>) => string;
|
|
9
|
+
export declare function apply(ctx: Context): void;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export type CopyKey = keyof typeof zh;
|
|
2
|
+
export declare const zh: {
|
|
3
|
+
readonly 'entry.label': "语音笔记";
|
|
4
|
+
readonly 'page.title': "语音笔记";
|
|
5
|
+
readonly 'record.start': "开始录音";
|
|
6
|
+
readonly 'record.stop': "结束并转写";
|
|
7
|
+
readonly 'record.recording': "录音中";
|
|
8
|
+
readonly 'record.micDenied': "麦克风不可用——检查浏览器授权";
|
|
9
|
+
readonly 'list.empty': "还没有语音笔记。点「开始录音」录第一段。";
|
|
10
|
+
readonly 'list.status.recording': "录制中";
|
|
11
|
+
readonly 'list.status.transcribing': "转写中";
|
|
12
|
+
readonly 'list.status.ready': "已完成";
|
|
13
|
+
readonly 'list.status.failed': "失败";
|
|
14
|
+
readonly 'detail.transcript': "转写";
|
|
15
|
+
readonly 'detail.summary': "纪要";
|
|
16
|
+
readonly 'detail.audio': "音频分段";
|
|
17
|
+
readonly 'detail.noTranscript': "(暂无)";
|
|
18
|
+
readonly 'action.share': "分享";
|
|
19
|
+
readonly 'action.delete': "删除";
|
|
20
|
+
readonly 'action.refresh': "刷新";
|
|
21
|
+
readonly 'share.copied': "链接已复制";
|
|
22
|
+
readonly 'share.path': "分享页";
|
|
23
|
+
readonly 'group.today': "今天";
|
|
24
|
+
readonly 'group.yesterday': "昨天";
|
|
25
|
+
readonly 'group.earlier': "更早";
|
|
26
|
+
};
|
|
27
|
+
export declare const en: Record<CopyKey, string>;
|