claude-code-session-manager 0.140.0 → 0.142.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 +80 -70
- package/bin/cli.cjs +11 -0
- package/dist/assets/AppSettings-BcZgIvvY.js +2 -0
- package/dist/assets/{History--JwsFnbn.js → History-DpZuJXi1.js} +1 -1
- package/dist/assets/HomeClaudeConfig-D32ZJtU-.js +3 -0
- package/dist/assets/{Panel-CALyU0pe.js → Panel-BjdJjhqt.js} +1 -1
- package/dist/assets/{ProjectClaudeConfig-sdlA-fUb.js → ProjectClaudeConfig-BTClYV6i.js} +7 -7
- package/dist/assets/Scheduler-DPobKx1Z.js +16 -0
- package/dist/assets/{SkillReferenceGraph-CxAuwhNN.js → SkillReferenceGraph-Cqyu4mef.js} +1 -1
- package/dist/assets/{Skills-DtRzghn5.js → Skills-vv0noNVI.js} +5 -5
- package/dist/assets/{TiptapBody-DApeu7x8.js → TiptapBody-CXi8Pvut.js} +1 -1
- package/dist/assets/Toggle-CiYCWLjh.js +1 -0
- package/dist/assets/{VoiceButton-DtStfv1H.js → VoiceButton-4NKKd1sC.js} +1 -1
- package/dist/assets/{historyTotals-ydfnwepc.js → historyTotals-DDunb8Ch.js} +1 -1
- package/dist/assets/index-Cn5E9958.js +3133 -0
- package/dist/assets/{index-DKXdXfka.css → index-DVHIHoJf.css} +1 -1
- package/dist/index.html +2 -2
- package/dist-web/app.css +1 -1
- package/dist-web/app.js +496 -484
- package/package.json +4 -2
- package/plugins/AGENTS.md +4 -3
- package/plugins/session-manager-dev/hooks/hooks.json +15 -0
- package/plugins/session-manager-dev/hooks/residue-check.cjs +241 -0
- package/plugins/session-manager-dev/skills/builder/SKILL.md +4 -2
- package/plugins/session-manager-dev/skills/develop/SKILL.md +55 -18
- package/plugins/session-manager-dev/skills/develop/standards.md +3 -2
- package/scripts/README.md +1 -0
- package/scripts/hooks/guard-tool-risk.cjs +5 -1
- package/scripts/hooks/lib/guard-tool-risk-policy.cjs +158 -1
- package/scripts/scheduler-mcp-server.cjs +737 -110
- package/src/main/adminRouteRegistry.cjs +59 -0
- package/src/main/agentLibrary.cjs +72 -4
- package/src/main/agentsIpc.cjs +3 -2
- package/src/main/build-info.json +4 -4
- package/src/main/chatRunner.cjs +231 -16
- package/src/main/config.cjs +30 -0
- package/src/main/docEdit.cjs +4 -2
- package/src/main/health.cjs +91 -0
- package/src/main/index.cjs +98 -48
- package/src/main/ipcSchemas.cjs +171 -8
- package/src/main/lib/AGENTS.md +1 -1
- package/src/main/lib/chatBootRecovery.cjs +2 -0
- package/src/main/lib/claude/agentModelResolve.cjs +51 -25
- package/src/main/lib/claude/agentSnapshot.cjs +186 -0
- package/src/main/lib/claude/environmentProfiles.cjs +242 -0
- package/src/main/lib/claude/jobSandboxPolicy.cjs +241 -33
- package/src/main/lib/claude/personaToolScope.cjs +82 -31
- package/src/main/lib/claude/runClaudeP.cjs +4 -2
- package/src/main/lib/claude/toolRiskMarks.cjs +41 -1
- package/src/main/lib/claude/usageCircuit.cjs +49 -21
- package/src/main/lib/claude/vaultRefs.cjs +277 -0
- package/src/main/lib/demo-video-kit/capture.cjs +282 -12
- package/src/main/lib/demo-video-kit/cast.md +43 -0
- package/src/main/lib/demo-video-kit/character.cjs +722 -30
- package/src/main/lib/demo-video-kit/checks.cjs +120 -1
- package/src/main/lib/demo-video-kit/export.cjs +218 -0
- package/src/main/lib/demo-video-kit/frames.cjs +104 -9
- package/src/main/lib/demo-video-kit/kind-marketing.md +121 -0
- package/src/main/lib/demo-video-kit/kind-tiktok.md +99 -0
- package/src/main/lib/demo-video-kit/kokoroSay.cjs +205 -0
- package/src/main/lib/demo-video-kit/project-home-html.md +43 -0
- package/src/main/lib/demo-video-kit/resumableRun.cjs +298 -0
- package/src/main/lib/demo-video-kit/seedKit.cjs +3 -3
- package/src/main/lib/demo-video-kit/voiceLane.cjs +233 -0
- package/src/main/lib/demo-video-kit/voiceLibrary.cjs +312 -0
- package/src/main/lib/epic/artifactAdminRoutes.cjs +211 -0
- package/src/main/lib/epic/artifactIpc.cjs +153 -0
- package/src/main/lib/epic/artifactNotice.cjs +72 -0
- package/src/main/lib/epic/artifactRetention.cjs +310 -0
- package/src/main/lib/epic/artifactStore.cjs +392 -0
- package/src/main/lib/epic/delegationReadiness.cjs +76 -2
- package/src/main/lib/epic/epicAgentRefresh.cjs +97 -0
- package/src/main/lib/epic/epicEventLog.cjs +214 -0
- package/src/main/lib/epic/epicMint.cjs +43 -1
- package/src/main/lib/epic/epicOutsideRead.cjs +1 -0
- package/src/main/lib/epic/epicOutsideState.cjs +18 -4
- package/src/main/lib/epic/epicPurge.cjs +347 -0
- package/src/main/lib/epic/epicReopen.cjs +5 -2
- package/src/main/lib/epic/epicSettle.cjs +4 -13
- package/src/main/lib/epic/epicSpawnPlan.cjs +57 -4
- package/src/main/lib/epic/epicStatusMirror.cjs +34 -1
- package/src/main/lib/epic/epicTransitions.cjs +139 -0
- package/src/main/lib/epic/epicValidationHook.cjs +81 -2
- package/src/main/lib/epic/guardSpawnSettings.cjs +3 -2
- package/src/main/lib/externalContract.cjs +37 -4
- package/src/main/lib/feedback-remote/crossProjectReply.cjs +389 -126
- package/src/main/lib/feedback-remote/customerFeedbackAdminRoutes.cjs +16 -3
- package/src/main/lib/feedback-remote/customerFeedbackClient.cjs +11 -6
- package/src/main/lib/feedback-remote/customerFeedbackInbox.cjs +26 -1
- package/src/main/lib/feedback-remote/customerFeedbackIpc.cjs +5 -2
- package/src/main/lib/feedback-remote/feedbackBoxDrain.cjs +409 -0
- package/src/main/lib/feedback-remote/feedbackBoxKeys.cjs +218 -0
- package/src/main/lib/feedback-remote/feedbackEnvelope.cjs +163 -0
- package/src/main/lib/feedback-remote/feedbackRelayClient.cjs +164 -0
- package/src/main/lib/feedback-remote/feedbackReport.cjs +234 -0
- package/src/main/lib/feedback-remote/feedbackSeal.cjs +217 -0
- package/src/main/lib/feedback-remote/localAdminHttp.cjs +108 -2
- package/src/main/lib/feedback-remote/macroAdminRoutes.cjs +2 -1
- package/src/main/lib/feedback-remote/needsYouIpc.cjs +17 -0
- package/src/main/lib/feedback-remote/needsYouStore.cjs +177 -6
- package/src/main/lib/feedback-remote/pendingReplies.cjs +189 -0
- package/src/main/lib/feedback-remote/projectHomeAdminRoutes.cjs +188 -10
- package/src/main/lib/feedback-remote/serviceClient.cjs +429 -0
- package/src/main/lib/feedback-remote/serviceMcpSession.cjs +252 -0
- package/src/main/lib/feedback-remote/serviceMcpTools.cjs +284 -0
- package/src/main/lib/feedback-remote/servicePairings.cjs +313 -0
- package/src/main/lib/feedback-remote/serviceStdioBridge.cjs +290 -0
- package/src/main/lib/feedback-remote/siteFeedbackAdminRoutes.cjs +295 -0
- package/src/main/lib/feedback-remote/siteFeedbackInbox.cjs +598 -0
- package/src/main/lib/feedback-remote/siteFeedbackIpc.cjs +86 -0
- package/src/main/lib/feedback-remote/siteFeedbackService.cjs +477 -0
- package/src/main/lib/git/worktreeDeps.cjs +77 -2
- package/src/main/lib/mcpToolCatalog.cjs +315 -30
- package/src/main/lib/opsOwnership.cjs +29 -1
- package/src/main/lib/opsRetentionSettings.cjs +27 -5
- package/src/main/lib/persona-macro/builtinMacros.cjs +7 -2
- package/src/main/lib/persona-macro/macroJobDispatch.cjs +52 -8
- package/src/main/lib/persona-macro/macroJobIpc.cjs +16 -6
- package/src/main/lib/persona-macro/macroLibrary.cjs +49 -7
- package/src/main/lib/persona-macro/personaFrontmatterSchema.cjs +83 -0
- package/src/main/lib/persona-macro/projectVideoToolCheck.cjs +3 -0
- package/src/main/lib/persona-macro/shippedPersonaSeeds.cjs +263 -0
- package/src/main/lib/phaseProjection.cjs +103 -0
- package/src/main/lib/prd/depSlugResolve.cjs +4 -13
- package/src/main/lib/prd/prdBodyPatch.cjs +12 -17
- package/src/main/lib/prd/prdCreate.cjs +67 -5
- package/src/main/lib/prd/prdCriteria.cjs +225 -0
- package/src/main/lib/prd/prdFrontmatter.cjs +68 -3
- package/src/main/lib/prd/prdGateFiles.cjs +125 -49
- package/src/main/lib/prd/prdSizing.cjs +3 -1
- package/src/main/lib/process/cleanEnv.cjs +52 -1
- package/src/main/lib/process/leftoverRenice.cjs +178 -0
- package/src/main/lib/process/memoryScope.cjs +31 -3
- package/src/main/lib/project-pages/pagesLibrary.cjs +72 -6
- package/src/main/lib/project-pages/videoReviewIpc.cjs +3 -2
- package/src/main/lib/project-pages/voiceAdminRoutes.cjs +175 -0
- package/src/main/lib/project-pages/voicePreview.cjs +196 -0
- package/src/main/lib/project-pages/voicesIpc.cjs +186 -0
- package/src/main/lib/promptSessionSchema.cjs +6 -0
- package/src/main/lib/scheduler/cancelledRow.cjs +57 -0
- package/src/main/lib/scheduler/cpuSample.cjs +133 -42
- package/src/main/lib/scheduler/criteriaChecks.cjs +169 -0
- package/src/main/lib/scheduler/definitionOfDone.cjs +44 -5
- package/src/main/lib/scheduler/jobMemoryStats.cjs +313 -0
- package/src/main/lib/scheduler/loadGate.cjs +82 -4
- package/src/main/lib/scheduler/planRecordHarvest.cjs +32 -2
- package/src/main/lib/scheduler/prdOutcomeRecord.cjs +133 -0
- package/src/main/lib/scheduler/preflightCheck.cjs +97 -0
- package/src/main/lib/scheduler/reviewNotice.cjs +39 -0
- package/src/main/lib/scheduler/runLogRetention.cjs +42 -1
- package/src/main/lib/scheduler/scheduleJobSchema.cjs +5 -0
- package/src/main/lib/scheduler/scheduleJobTransitions.cjs +47 -0
- package/src/main/lib/scheduler/schedulerConfig.cjs +47 -0
- package/src/main/lib/scheduler/schedulerPaths.cjs +0 -3
- package/src/main/lib/scheduler/schedulerRuntimeState.cjs +50 -1
- package/src/main/lib/scheduler/sessionSlots.cjs +5 -1
- package/src/main/lib/scheduler/slotsRoute.cjs +237 -0
- package/src/main/lib/scheduler/toolClearance.cjs +258 -0
- package/src/main/lib/scheduler/validatorVerdictSchema.cjs +140 -10
- package/src/main/lib/tasks/memoryConsolidation.cjs +300 -0
- package/src/main/lib/tasks/mitigationReview.cjs +294 -0
- package/src/main/lib/tasks/taskAdminRoutes.cjs +312 -22
- package/src/main/lib/tasks/taskIpc.cjs +22 -18
- package/src/main/lib/tasks/taskLedger.cjs +147 -25
- package/src/main/lib/tasks/taskRun.cjs +186 -17
- package/src/main/lib/tasks/taskSwitch.cjs +85 -12
- package/src/main/lib/tasks/taskSwitchRoute.cjs +172 -0
- package/src/main/lib/telemetry/auditEvents.cjs +207 -6
- package/src/main/lib/telemetry/eventsAdminRoutes.cjs +198 -6
- package/src/main/lib/telemetry/webhookDelivery.cjs +205 -0
- package/src/main/lib/telemetry/webhookRegistry.cjs +281 -0
- package/src/main/lib/toolUseClassify.cjs +181 -0
- package/src/main/lib/util/smfileRange.cjs +77 -0
- package/src/main/projectPages.cjs +3 -1
- package/src/main/promptSessionEvents.cjs +5 -0
- package/src/main/pty.cjs +68 -0
- package/src/main/queueOps.cjs +32 -13
- package/src/main/runVerify.cjs +74 -9
- package/src/main/scheduler/AGENTS.md +1 -0
- package/src/main/scheduler/budget.cjs +6 -5
- package/src/main/scheduler/prdParser.cjs +62 -14
- package/src/main/scheduler/reviewLadder.cjs +45 -28
- package/src/main/scheduler/starvation.cjs +69 -0
- package/src/main/scheduler/utilizationHold.cjs +101 -0
- package/src/main/scheduler.cjs +1745 -210
- package/src/main/templates/PRD_AUTHORING.md +13 -2
- package/src/main/webRemote.cjs +463 -6
- package/src/preload/api.d.ts +529 -38
- package/src/preload/apiSurface.json +209 -7
- package/src/preload/index.cjs +46 -1
- package/src/seed/agents/architect.md +6 -1
- package/src/seed/agents/blender-artist.md +2 -1
- package/src/seed/agents/competitive-analyst.md +3 -1
- package/src/seed/agents/competitive-researcher.md +2 -1
- package/src/seed/agents/demo-video-builder.md +87 -83
- package/src/seed/agents/dev-lead.md +3 -3
- package/src/seed/agents/project-home-builder.md +9 -31
- package/src/seed/agents/validator.md +9 -10
- package/dist/assets/AppSettings-CMFEM68R.js +0 -2
- package/dist/assets/HomeClaudeConfig-ijEXs--3.js +0 -3
- package/dist/assets/Scheduler-YT3j8SUa.js +0 -16
- package/dist/assets/ViewTabs-B7qLXRAH.js +0 -1
- package/dist/assets/index-CtxruO1L.js +0 -3121
- package/src/main/lib/epic/epicStateWatcher.cjs +0 -157
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Demo-video kit voice lane: a cross-process FIFO so at most one heavy voice
|
|
3
|
+
// synthesis (and, separately, one light one) runs machine-wide at a time.
|
|
4
|
+
//
|
|
5
|
+
// <lane dir>/<heavy|light>/<15-digit epoch ms>-<pid>-<6 hex>.json
|
|
6
|
+
//
|
|
7
|
+
// lane dir = SM_VOICE_LANE_DIR, else <voices home>/lane, where voices home =
|
|
8
|
+
// SM_VOICES_HOME or ~/.claude/session-manager/voices. Order and liveness come
|
|
9
|
+
// from the FILE NAME (ts, pid) only, so a reader never depends on a half-written
|
|
10
|
+
// body; the body {voiceId,label,projectRoot,kind,since} is informational.
|
|
11
|
+
// Position 0 = holder. Tickets whose pid is dead or older than 6 h are swept.
|
|
12
|
+
// Every leaveLane appends one usage line to <voices home>/usage.jsonl.
|
|
13
|
+
//
|
|
14
|
+
// Kit files are seeded to ~/.claude/session-manager/demo-video-kit/ and must
|
|
15
|
+
// load standalone: node built-ins only (no ../util/atomicFs.cjs, no
|
|
16
|
+
// ../process/pidAlive.cjs), hence the local exclusive-create + pid check.
|
|
17
|
+
const fs = require('node:fs');
|
|
18
|
+
const path = require('node:path');
|
|
19
|
+
const crypto = require('node:crypto');
|
|
20
|
+
const { voicesHome } = require('./voiceLibrary.cjs');
|
|
21
|
+
|
|
22
|
+
const LANES = Object.freeze(['heavy', 'light']);
|
|
23
|
+
const STALE_MS = 6 * 60 * 60 * 1000;
|
|
24
|
+
const USAGE_MAX_BYTES = 2 * 1024 * 1024;
|
|
25
|
+
const DEFAULT_POLL_MS = 500;
|
|
26
|
+
let lastTs = 0;
|
|
27
|
+
const TICKET_RE = /^(\d{15})-(\d+)-([0-9a-f]{6})\.json$/;
|
|
28
|
+
|
|
29
|
+
function laneDir() {
|
|
30
|
+
return process.env.SM_VOICE_LANE_DIR || path.join(voicesHome(), 'lane');
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function usagePath() {
|
|
34
|
+
return path.join(voicesHome(), 'usage.jsonl');
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function checkLane(name) {
|
|
38
|
+
if (!LANES.includes(name)) throw new Error(`voiceLane: unknown lane ${JSON.stringify(name)} (heavy|light)`);
|
|
39
|
+
return name;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** entry.heavy (boolean) wins; else kokoro voices are light, every other engine heavy. */
|
|
43
|
+
function laneFor(voice, entry) {
|
|
44
|
+
if (entry && typeof entry.heavy === 'boolean') return entry.heavy ? 'heavy' : 'light';
|
|
45
|
+
return voice && voice.engine === 'kokoro' ? 'light' : 'heavy';
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function pidAlive(pid) {
|
|
49
|
+
if (!Number.isInteger(pid) || pid <= 0) return false;
|
|
50
|
+
try { process.kill(pid, 0); return true; } catch (err) { return !!err && err.code === 'EPERM'; }
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function sleepSync(ms) {
|
|
54
|
+
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, Math.max(1, ms));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Join a lane. opts: {lane?, voice?, entry?, voiceId, label, projectRoot, kind}.
|
|
59
|
+
* lane defaults to laneFor(voice, entry). Returns the ticket handle.
|
|
60
|
+
*/
|
|
61
|
+
function enterLane(opts = {}) {
|
|
62
|
+
const lane = checkLane(opts.lane || laneFor(opts.voice, opts.entry));
|
|
63
|
+
const dir = path.join(laneDir(), lane);
|
|
64
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
65
|
+
const voiceId = opts.voiceId != null ? String(opts.voiceId) : (opts.voice && opts.voice.id ? String(opts.voice.id) : null);
|
|
66
|
+
const label = opts.label != null ? String(opts.label) : (opts.voice && opts.voice.label ? String(opts.voice.label) : voiceId);
|
|
67
|
+
const projectRoot = opts.projectRoot != null ? String(opts.projectRoot) : null;
|
|
68
|
+
const kind = opts.kind != null ? String(opts.kind) : null;
|
|
69
|
+
for (let attempt = 0; ; attempt++) {
|
|
70
|
+
// Strictly increasing per process, so two entries from one process keep FIFO
|
|
71
|
+
// order even within the same millisecond (the 6-hex suffix is random).
|
|
72
|
+
const ts = Math.max(Date.now(), lastTs + 1);
|
|
73
|
+
lastTs = ts;
|
|
74
|
+
const name = `${String(ts).padStart(15, '0')}-${process.pid}-${crypto.randomBytes(3).toString('hex')}.json`;
|
|
75
|
+
const file = path.join(dir, name);
|
|
76
|
+
const since = new Date(ts).toISOString();
|
|
77
|
+
try {
|
|
78
|
+
fs.writeFileSync(file, JSON.stringify({ voiceId, label, projectRoot, kind, since }), { flag: 'wx' });
|
|
79
|
+
return { lane, name, file, ts, pid: process.pid, voiceId, label, projectRoot, kind, since, enteredAt: ts, grantedAt: null };
|
|
80
|
+
} catch (err) {
|
|
81
|
+
if (err && err.code === 'EEXIST' && attempt < 5) continue;
|
|
82
|
+
throw err;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function readBody(file) {
|
|
88
|
+
try { const j = JSON.parse(fs.readFileSync(file, 'utf8')); return j && typeof j === 'object' ? j : {}; } catch { return {}; }
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** Live tickets of one lane in FIFO order, sweeping dead/stale ones (never `keepName`). */
|
|
92
|
+
function liveTickets(lane, keepName) {
|
|
93
|
+
const dir = path.join(laneDir(), lane);
|
|
94
|
+
let names;
|
|
95
|
+
try { names = fs.readdirSync(dir); } catch { return []; }
|
|
96
|
+
const now = Date.now();
|
|
97
|
+
const live = [];
|
|
98
|
+
for (const name of names) {
|
|
99
|
+
const m = TICKET_RE.exec(name);
|
|
100
|
+
if (!m) continue;
|
|
101
|
+
const ts = Number(m[1]);
|
|
102
|
+
const pid = Number(m[2]);
|
|
103
|
+
if (name !== keepName && (now - ts > STALE_MS || !pidAlive(pid))) {
|
|
104
|
+
try { fs.unlinkSync(path.join(dir, name)); } catch { /* another reader swept it */ }
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
live.push({ name, ts, pid, file: path.join(dir, name) });
|
|
108
|
+
}
|
|
109
|
+
live.sort((a, b) => (a.ts - b.ts) || (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
110
|
+
return live;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** {position, total, ahead:[{voiceId,label,since}]}; position 0 = holder, -1 = ticket gone. */
|
|
114
|
+
function lanePosition(ticket) {
|
|
115
|
+
const lane = checkLane(ticket.lane);
|
|
116
|
+
const live = liveTickets(lane, ticket.name);
|
|
117
|
+
const idx = live.findIndex((t) => t.name === ticket.name);
|
|
118
|
+
if (idx < 0) return { position: -1, total: live.length, ahead: [] };
|
|
119
|
+
const ahead = live.slice(0, idx).map((t) => {
|
|
120
|
+
const b = readBody(t.file);
|
|
121
|
+
return { voiceId: b.voiceId ?? null, label: b.label ?? null, since: b.since ?? new Date(t.ts).toISOString() };
|
|
122
|
+
});
|
|
123
|
+
return { position: idx, total: live.length, ahead };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Block synchronously until the ticket holds the lane. timeoutMs is required.
|
|
128
|
+
* onPosition(pos) fires on every position change. On timeout the ticket leaves
|
|
129
|
+
* the lane (no usage line) and {ok:false,error} names the last position.
|
|
130
|
+
*/
|
|
131
|
+
function waitTurn(ticket, { timeoutMs, pollMs = DEFAULT_POLL_MS, onPosition } = {}) {
|
|
132
|
+
if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) throw new Error('voiceLane.waitTurn: timeoutMs is required (no unbounded wait)');
|
|
133
|
+
const deadline = Date.now() + timeoutMs;
|
|
134
|
+
let last = null;
|
|
135
|
+
for (;;) {
|
|
136
|
+
const pos = lanePosition(ticket);
|
|
137
|
+
if (pos.position === -1) {
|
|
138
|
+
return { ok: false, error: `voice lane ${ticket.lane}: ticket ${ticket.name} disappeared while waiting`, position: -1 };
|
|
139
|
+
}
|
|
140
|
+
if (last === null || pos.position !== last) {
|
|
141
|
+
last = pos.position;
|
|
142
|
+
if (typeof onPosition === 'function') { try { onPosition(pos); } catch { /* caller's callback */ } }
|
|
143
|
+
}
|
|
144
|
+
if (pos.position === 0) {
|
|
145
|
+
ticket.grantedAt = Date.now();
|
|
146
|
+
return { ok: true, position: 0, waitedMs: ticket.grantedAt - ticket.enteredAt };
|
|
147
|
+
}
|
|
148
|
+
const left = deadline - Date.now();
|
|
149
|
+
if (left <= 0) {
|
|
150
|
+
leaveLane(ticket, { ok: false, record: false });
|
|
151
|
+
return { ok: false, error: `voice lane ${ticket.lane}: timed out after ${timeoutMs} ms at position ${pos.position} of ${pos.total}`, position: pos.position };
|
|
152
|
+
}
|
|
153
|
+
sleepSync(Math.min(pollMs, left));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function appendUsage(line) {
|
|
158
|
+
const p = usagePath();
|
|
159
|
+
fs.mkdirSync(path.dirname(p), { recursive: true });
|
|
160
|
+
try {
|
|
161
|
+
if (fs.statSync(p).size > USAGE_MAX_BYTES) fs.renameSync(p, path.join(path.dirname(p), 'usage.1.jsonl'));
|
|
162
|
+
} catch { /* no usage file yet */ }
|
|
163
|
+
fs.appendFileSync(p, JSON.stringify(line) + '\n');
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Remove the ticket and (unless record:false) append one usage line
|
|
168
|
+
* {at,voiceId,projectRoot,lane,kind,ok,audioSeconds,wallMs,waitedMs}.
|
|
169
|
+
*/
|
|
170
|
+
function leaveLane(ticket, { ok = true, audioSeconds = null, record = true } = {}) {
|
|
171
|
+
try { fs.unlinkSync(ticket.file); } catch { /* already swept */ }
|
|
172
|
+
if (!record) return null;
|
|
173
|
+
const now = Date.now();
|
|
174
|
+
const granted = ticket.grantedAt || ticket.enteredAt;
|
|
175
|
+
const line = {
|
|
176
|
+
at: new Date(now).toISOString(),
|
|
177
|
+
voiceId: ticket.voiceId ?? null,
|
|
178
|
+
projectRoot: ticket.projectRoot ?? null,
|
|
179
|
+
lane: ticket.lane,
|
|
180
|
+
kind: ticket.kind ?? null,
|
|
181
|
+
ok: !!ok,
|
|
182
|
+
audioSeconds: Number.isFinite(audioSeconds) ? audioSeconds : null,
|
|
183
|
+
wallMs: now - granted,
|
|
184
|
+
waitedMs: granted - ticket.enteredAt,
|
|
185
|
+
};
|
|
186
|
+
try { appendUsage(line); } catch { /* usage is best-effort; never fail a synthesis */ }
|
|
187
|
+
return line;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** {heavy:[...], light:[...]} each {voiceId,label,projectRoot,pid,since,position}. */
|
|
191
|
+
function laneSnapshot() {
|
|
192
|
+
const out = {};
|
|
193
|
+
for (const lane of LANES) {
|
|
194
|
+
out[lane] = liveTickets(lane, null).map((t, position) => {
|
|
195
|
+
const b = readBody(t.file);
|
|
196
|
+
return { voiceId: b.voiceId ?? null, label: b.label ?? null, projectRoot: b.projectRoot ?? null, pid: t.pid, since: b.since ?? new Date(t.ts).toISOString(), position };
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function readLines(file) {
|
|
203
|
+
let text;
|
|
204
|
+
try { text = fs.readFileSync(file, 'utf8'); } catch { return []; }
|
|
205
|
+
const out = [];
|
|
206
|
+
for (const raw of text.split('\n')) {
|
|
207
|
+
if (!raw.trim()) continue;
|
|
208
|
+
try { const j = JSON.parse(raw); if (j && typeof j === 'object') out.push(j); } catch { /* skip */ }
|
|
209
|
+
}
|
|
210
|
+
return out;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Usage lines for one project (all when projectRoot omitted), newest first. */
|
|
214
|
+
function readUsage({ projectRoot, limit = 100 } = {}) {
|
|
215
|
+
const p = usagePath();
|
|
216
|
+
const all = [...readLines(path.join(path.dirname(p), 'usage.1.jsonl')), ...readLines(p)];
|
|
217
|
+
const rows = projectRoot == null ? all : all.filter((r) => r.projectRoot === projectRoot);
|
|
218
|
+
rows.reverse();
|
|
219
|
+
return Number.isFinite(limit) && limit >= 0 ? rows.slice(0, limit) : rows;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
module.exports = {
|
|
223
|
+
laneFor,
|
|
224
|
+
enterLane,
|
|
225
|
+
lanePosition,
|
|
226
|
+
waitTurn,
|
|
227
|
+
leaveLane,
|
|
228
|
+
laneSnapshot,
|
|
229
|
+
readUsage,
|
|
230
|
+
laneDir,
|
|
231
|
+
voicesHome,
|
|
232
|
+
LANES,
|
|
233
|
+
};
|
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
// Demo-video kit voice library: the ONE resolver for library voices.
|
|
3
|
+
// Three scopes, highest first (like agents):
|
|
4
|
+
// project <projectRoot>/.claude/voices/<id>.json
|
|
5
|
+
// machine <voicesHome>/library/<id>.json
|
|
6
|
+
// builtin every voice in the installed Kokoro model (voices-v1.0.bin zip),
|
|
7
|
+
// or KOKORO_V1_VOICES (installed:false) when the model is absent.
|
|
8
|
+
// A higher scope shadows a lower one with the same id. Approvals live in
|
|
9
|
+
// <voicesHome>/approvals.json; this module only READS them. An approved voice
|
|
10
|
+
// whose pin no longer matches, or whose file is gone, is refused — never used,
|
|
11
|
+
// never replaced by a lower scope.
|
|
12
|
+
// Kit files are seeded to ~/.claude/session-manager/demo-video-kit/ and must
|
|
13
|
+
// load standalone: node built-ins and this folder only.
|
|
14
|
+
const fs = require('node:fs');
|
|
15
|
+
const os = require('node:os');
|
|
16
|
+
const path = require('node:path');
|
|
17
|
+
const crypto = require('node:crypto');
|
|
18
|
+
const { validateCharacterJson } = require('./character.cjs');
|
|
19
|
+
|
|
20
|
+
/** The one library voice id pattern; every other copy points here. */
|
|
21
|
+
const VOICE_ID_RE = /^[a-z0-9][a-z0-9_-]{0,63}$/;
|
|
22
|
+
const MAX_ENTRY_BYTES = 16 * 1024;
|
|
23
|
+
const LABEL_MAX = 80;
|
|
24
|
+
const SAMPLE_MAX = 300;
|
|
25
|
+
const LICENCE_MAX = 120;
|
|
26
|
+
const PIN_FILES_MAX = 8;
|
|
27
|
+
const DEFAULT_SAMPLE = 'Hello! This is how I sound when I tell your project story.';
|
|
28
|
+
const BUILTIN_LICENCE = 'Apache-2.0 (Kokoro-82M)';
|
|
29
|
+
const SCOPE_ORDER = Object.freeze({ project: 0, machine: 1, builtin: 2 });
|
|
30
|
+
|
|
31
|
+
const KOKORO_V1_VOICES = Object.freeze([
|
|
32
|
+
'af_alloy', 'af_aoede', 'af_bella', 'af_heart', 'af_jessica', 'af_kore', 'af_nicole', 'af_nova', 'af_river', 'af_sarah', 'af_sky',
|
|
33
|
+
'am_adam', 'am_echo', 'am_eric', 'am_fenrir', 'am_liam', 'am_michael', 'am_onyx', 'am_puck', 'am_santa',
|
|
34
|
+
'bf_alice', 'bf_emma', 'bf_isabella', 'bf_lily', 'bm_daniel', 'bm_fable', 'bm_george', 'bm_lewis',
|
|
35
|
+
'ef_dora', 'em_alex', 'em_santa', 'ff_siwis', 'hf_alpha', 'hf_beta', 'hm_omega', 'hm_psi',
|
|
36
|
+
'if_sara', 'im_nicola', 'jf_alpha', 'jf_gongitsune', 'jf_nezumi', 'jf_tebukuro', 'jm_kumo',
|
|
37
|
+
'pf_dora', 'pm_alex', 'pm_santa', 'zf_xiaobei', 'zf_xiaoni', 'zf_xiaoxiao', 'zf_xiaoyi',
|
|
38
|
+
'zm_yunjian', 'zm_yunxi', 'zm_yunxia', 'zm_yunyang',
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
const KOKORO_LANGS = Object.freeze({
|
|
42
|
+
a: 'US', b: 'UK', e: 'Spanish', f: 'French', h: 'Hindi', i: 'Italian', j: 'Japanese', p: 'Brazilian Portuguese', z: 'Mandarin',
|
|
43
|
+
});
|
|
44
|
+
const KOKORO_GENDERS = Object.freeze({ f: 'female', m: 'male' });
|
|
45
|
+
|
|
46
|
+
function voicesHome() {
|
|
47
|
+
return process.env.SM_VOICES_HOME || path.join(os.homedir(), '.claude', 'session-manager', 'voices');
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function kokoroModelDir() {
|
|
51
|
+
return process.env.KOKORO_MODEL_DIR || path.join(os.homedir(), '.cache', 'kokoro-onnx');
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function projectVoicesDir(projectRoot) {
|
|
55
|
+
return projectRoot ? path.join(path.resolve(projectRoot), '.claude', 'voices') : null;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function machineVoicesDir() {
|
|
59
|
+
return path.join(voicesHome(), 'library');
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** 'af_heart' -> 'Heart (US female)'. */
|
|
63
|
+
function builtinLabel(id) {
|
|
64
|
+
const m = /^([a-z])([a-z])_(.+)$/.exec(id);
|
|
65
|
+
if (!m) return id;
|
|
66
|
+
const name = m[3].charAt(0).toUpperCase() + m[3].slice(1);
|
|
67
|
+
const lang = KOKORO_LANGS[m[1]];
|
|
68
|
+
const gender = KOKORO_GENDERS[m[2]];
|
|
69
|
+
const tags = [lang, gender].filter(Boolean).join(' ');
|
|
70
|
+
return tags ? `${name} (${tags})` : name;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Entry names in a zip's central directory; throws on a non-zip. Reads only the tail + directory. */
|
|
74
|
+
function zipEntryNames(file) {
|
|
75
|
+
const fd = fs.openSync(file, 'r');
|
|
76
|
+
try {
|
|
77
|
+
const size = fs.fstatSync(fd).size;
|
|
78
|
+
const tailLen = Math.min(size, 65536 + 22);
|
|
79
|
+
const tail = Buffer.alloc(tailLen);
|
|
80
|
+
fs.readSync(fd, tail, 0, tailLen, size - tailLen);
|
|
81
|
+
let eocd = -1;
|
|
82
|
+
for (let i = tailLen - 22; i >= 0; i--) {
|
|
83
|
+
if (tail.readUInt32LE(i) === 0x06054b50) { eocd = i; break; }
|
|
84
|
+
}
|
|
85
|
+
if (eocd === -1) throw new Error('no zip end-of-central-directory record');
|
|
86
|
+
const count = tail.readUInt16LE(eocd + 10);
|
|
87
|
+
const cdSize = tail.readUInt32LE(eocd + 12);
|
|
88
|
+
const cdOffset = tail.readUInt32LE(eocd + 16);
|
|
89
|
+
if (cdOffset + cdSize > size) throw new Error('zip central directory out of range');
|
|
90
|
+
const cd = Buffer.alloc(cdSize);
|
|
91
|
+
fs.readSync(fd, cd, 0, cdSize, cdOffset);
|
|
92
|
+
const names = [];
|
|
93
|
+
let p = 0;
|
|
94
|
+
for (let n = 0; n < count; n++) {
|
|
95
|
+
if (p + 46 > cd.length || cd.readUInt32LE(p) !== 0x02014b50) throw new Error('bad zip central directory entry');
|
|
96
|
+
const nameLen = cd.readUInt16LE(p + 28);
|
|
97
|
+
const extraLen = cd.readUInt16LE(p + 30);
|
|
98
|
+
const commentLen = cd.readUInt16LE(p + 32);
|
|
99
|
+
names.push(cd.toString('utf8', p + 46, p + 46 + nameLen));
|
|
100
|
+
p += 46 + nameLen + extraLen + commentLen;
|
|
101
|
+
}
|
|
102
|
+
return names;
|
|
103
|
+
} finally {
|
|
104
|
+
fs.closeSync(fd);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function builtinEntry(id, installed) {
|
|
109
|
+
return {
|
|
110
|
+
id,
|
|
111
|
+
label: builtinLabel(id),
|
|
112
|
+
voice: { engine: 'kokoro', id },
|
|
113
|
+
engine: 'kokoro',
|
|
114
|
+
sampleText: DEFAULT_SAMPLE,
|
|
115
|
+
licence: BUILTIN_LICENCE,
|
|
116
|
+
heavy: false,
|
|
117
|
+
scope: 'builtin',
|
|
118
|
+
installed,
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function builtinVoices() {
|
|
123
|
+
let ids = null;
|
|
124
|
+
try {
|
|
125
|
+
const names = zipEntryNames(path.join(kokoroModelDir(), 'voices-v1.0.bin'));
|
|
126
|
+
ids = names.filter((n) => n.endsWith('.npy')).map((n) => path.posix.basename(n).slice(0, -4)).filter(Boolean);
|
|
127
|
+
if (ids.length === 0) ids = null;
|
|
128
|
+
} catch { /* missing or unreadable: fall back */ }
|
|
129
|
+
if (ids) return [...new Set(ids)].map((id) => builtinEntry(id, true));
|
|
130
|
+
return KOKORO_V1_VOICES.map((id) => builtinEntry(id, false));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** -> null when `json` is a valid voice entry for file stem `stem`, else a reason string. */
|
|
134
|
+
function validateVoiceEntry(json, stem) {
|
|
135
|
+
if (typeof stem !== 'string' || !VOICE_ID_RE.test(stem)) return `file name '${stem}' must match ${VOICE_ID_RE}`;
|
|
136
|
+
if (!json || typeof json !== 'object' || Array.isArray(json)) return 'voice entry must be an object';
|
|
137
|
+
if ('id' in json && json.id !== stem) return `id '${json.id}' must equal the file name '${stem}'`;
|
|
138
|
+
if ('label' in json && (typeof json.label !== 'string' || json.label.length < 1 || json.label.length > LABEL_MAX)) {
|
|
139
|
+
return `label must be a 1-${LABEL_MAX} char string`;
|
|
140
|
+
}
|
|
141
|
+
const v = json.voice;
|
|
142
|
+
if (!v || typeof v !== 'object' || Array.isArray(v)) return 'voice is required';
|
|
143
|
+
if ('voiceId' in v) return 'voice must be an inline voice, not a {voiceId} reference (no chains)';
|
|
144
|
+
const vErr = validateCharacterJson({ name: 'x', palette: ['x'], voice: v });
|
|
145
|
+
if (vErr) return vErr;
|
|
146
|
+
if ('sampleText' in json && (typeof json.sampleText !== 'string' || json.sampleText.length > SAMPLE_MAX)) {
|
|
147
|
+
return `sampleText must be a string of at most ${SAMPLE_MAX} chars`;
|
|
148
|
+
}
|
|
149
|
+
if ('licence' in json && (typeof json.licence !== 'string' || json.licence.length > LICENCE_MAX)) {
|
|
150
|
+
return `licence must be a string of at most ${LICENCE_MAX} chars`;
|
|
151
|
+
}
|
|
152
|
+
if ('heavy' in json && typeof json.heavy !== 'boolean') return 'heavy must be a boolean';
|
|
153
|
+
if ('pinFiles' in json) {
|
|
154
|
+
const pf = json.pinFiles;
|
|
155
|
+
if (!Array.isArray(pf) || pf.length > PIN_FILES_MAX) return `pinFiles must be an array of at most ${PIN_FILES_MAX} paths`;
|
|
156
|
+
for (const f of pf) {
|
|
157
|
+
if (typeof f !== 'string' || !f) return 'pinFiles items must be non-empty strings';
|
|
158
|
+
if (path.isAbsolute(f) || path.posix.isAbsolute(f) || path.win32.isAbsolute(f)) return `pinFiles '${f}' must be project-relative, not absolute`;
|
|
159
|
+
if (f.split(/[\\/]/).includes('..')) return `pinFiles '${f}' must not contain '..'`;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** -> { entry } | { error } | null (no file). */
|
|
166
|
+
function readEntryFile(file, scope) {
|
|
167
|
+
if (!fs.existsSync(file)) return null;
|
|
168
|
+
const stem = path.basename(file, '.json');
|
|
169
|
+
let json;
|
|
170
|
+
try {
|
|
171
|
+
const raw = fs.readFileSync(file, 'utf8');
|
|
172
|
+
if (Buffer.byteLength(raw, 'utf8') > MAX_ENTRY_BYTES) return { error: `exceeds the ${MAX_ENTRY_BYTES}-byte limit` };
|
|
173
|
+
json = JSON.parse(raw);
|
|
174
|
+
} catch (e) {
|
|
175
|
+
return { error: `not valid JSON: ${e.message}` };
|
|
176
|
+
}
|
|
177
|
+
const reason = validateVoiceEntry(json, stem);
|
|
178
|
+
if (reason) return { error: reason };
|
|
179
|
+
return {
|
|
180
|
+
entry: {
|
|
181
|
+
id: stem,
|
|
182
|
+
label: json.label || stem,
|
|
183
|
+
voice: json.voice,
|
|
184
|
+
engine: json.voice.engine,
|
|
185
|
+
sampleText: json.sampleText ?? DEFAULT_SAMPLE,
|
|
186
|
+
licence: json.licence ?? 'unknown',
|
|
187
|
+
heavy: json.heavy === true,
|
|
188
|
+
...(json.pinFiles ? { pinFiles: [...json.pinFiles] } : {}),
|
|
189
|
+
scope,
|
|
190
|
+
file,
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function listDir(dir) {
|
|
196
|
+
if (!dir) return [];
|
|
197
|
+
try {
|
|
198
|
+
return fs.readdirSync(dir).filter((f) => f.endsWith('.json')).sort().map((f) => path.join(dir, f));
|
|
199
|
+
} catch {
|
|
200
|
+
return [];
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function emptyApprovals() {
|
|
205
|
+
return { version: 1, approvals: {} };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** <voicesHome>/approvals.json; never throws (missing/corrupt = empty). */
|
|
209
|
+
function readApprovals() {
|
|
210
|
+
try {
|
|
211
|
+
const j = JSON.parse(fs.readFileSync(path.join(voicesHome(), 'approvals.json'), 'utf8'));
|
|
212
|
+
if (!j || typeof j !== 'object' || !j.approvals || typeof j.approvals !== 'object' || Array.isArray(j.approvals)) return emptyApprovals();
|
|
213
|
+
return { version: 1, approvals: j.approvals };
|
|
214
|
+
} catch {
|
|
215
|
+
return emptyApprovals();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/** approvals.json key: '<scope>|<resolved projectRoot, project scope only>|<id>'. The ONE copy — voicesIpc.cjs (the writer) imports it. */
|
|
220
|
+
function approvalKey(scope, projectRoot, id) {
|
|
221
|
+
return `${scope}|${scope === 'project' && projectRoot ? path.resolve(projectRoot) : ''}|${id}`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Key-sorted JSON (undefined keys dropped) — the stable bytes pinOf hashes. */
|
|
225
|
+
function canonical(v) {
|
|
226
|
+
if (Array.isArray(v)) return `[${v.map(canonical).join(',')}]`;
|
|
227
|
+
if (v && typeof v === 'object') {
|
|
228
|
+
return `{${Object.keys(v).sort().filter((k) => v[k] !== undefined).map((k) => `${JSON.stringify(k)}:${canonical(v[k])}`).join(',')}}`;
|
|
229
|
+
}
|
|
230
|
+
return JSON.stringify(v === undefined ? null : v);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
const sha256 = (b) => crypto.createHash('sha256').update(b).digest('hex');
|
|
234
|
+
|
|
235
|
+
/** sha256 of canonical {id, voice, pinFiles} + per pinFile sha256 of its bytes (or 'missing'). */
|
|
236
|
+
function pinOf(entry, projectRoot) {
|
|
237
|
+
const pinFiles = Array.isArray(entry.pinFiles) ? entry.pinFiles : [];
|
|
238
|
+
const h = crypto.createHash('sha256');
|
|
239
|
+
h.update(canonical({ id: entry.id, voice: entry.voice, pinFiles }));
|
|
240
|
+
for (const f of pinFiles) {
|
|
241
|
+
let part = 'missing';
|
|
242
|
+
try {
|
|
243
|
+
if (projectRoot) part = sha256(fs.readFileSync(path.join(path.resolve(projectRoot), f)));
|
|
244
|
+
} catch { /* missing */ }
|
|
245
|
+
h.update(part);
|
|
246
|
+
}
|
|
247
|
+
return h.digest('hex');
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
function withApproval(entry, projectRoot, approvals) {
|
|
251
|
+
const a = approvals.approvals[approvalKey(entry.scope, projectRoot, entry.id)];
|
|
252
|
+
const approved = !!(a && typeof a === 'object');
|
|
253
|
+
const pinOk = approved ? a.pin === pinOf(entry, projectRoot) : true;
|
|
254
|
+
return { ...entry, approved, pinOk };
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** -> { voices, errors }: every library voice, highest scope wins per id. */
|
|
258
|
+
function listVoices(projectRoot) {
|
|
259
|
+
const approvals = readApprovals();
|
|
260
|
+
const errors = [];
|
|
261
|
+
const byId = new Map();
|
|
262
|
+
const add = (entry) => { if (!byId.has(entry.id)) byId.set(entry.id, entry); };
|
|
263
|
+
for (const [scope, dir] of [['project', projectVoicesDir(projectRoot)], ['machine', machineVoicesDir()]]) {
|
|
264
|
+
for (const file of listDir(dir)) {
|
|
265
|
+
const r = readEntryFile(file, scope);
|
|
266
|
+
if (!r) continue;
|
|
267
|
+
if (r.error) errors.push({ file, error: r.error });
|
|
268
|
+
else add(r.entry);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
for (const b of builtinVoices()) add(b);
|
|
272
|
+
const voices = [...byId.values()]
|
|
273
|
+
.map((e) => withApproval(e, projectRoot, approvals))
|
|
274
|
+
.sort((a, b) => (SCOPE_ORDER[a.scope] - SCOPE_ORDER[b.scope]) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0));
|
|
275
|
+
return { voices, errors };
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** -> { ok:true, entry, voice, scope, approved } | { ok:false, error }. */
|
|
279
|
+
function resolveVoiceId(projectRoot, id) {
|
|
280
|
+
if (typeof id !== 'string' || !VOICE_ID_RE.test(id)) return { ok: false, error: `unknown voice '${id}'` };
|
|
281
|
+
const approvals = readApprovals();
|
|
282
|
+
let entry = null;
|
|
283
|
+
for (const [scope, dir] of [['project', projectVoicesDir(projectRoot)], ['machine', machineVoicesDir()]]) {
|
|
284
|
+
if (!dir) continue;
|
|
285
|
+
const r = readEntryFile(path.join(dir, `${id}.json`), scope);
|
|
286
|
+
if (r && r.error) return { ok: false, error: `voice '${id}' is invalid (${path.join(dir, `${id}.json`)}): ${r.error}` };
|
|
287
|
+
if (r) { entry = r.entry; break; }
|
|
288
|
+
if (approvals.approvals[approvalKey(scope, projectRoot, id)]) {
|
|
289
|
+
return { ok: false, error: `approved voice '${id}' was removed outside the Voices screen` };
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (!entry) entry = builtinVoices().find((b) => b.id === id) || null;
|
|
293
|
+
if (!entry) return { ok: false, error: `unknown voice '${id}'` };
|
|
294
|
+
const e = withApproval(entry, projectRoot, approvals);
|
|
295
|
+
if (e.approved && !e.pinOk) {
|
|
296
|
+
return { ok: false, error: `approved voice '${id}' changed since a human approved it; re-approve it in Project Home > Video > Voices` };
|
|
297
|
+
}
|
|
298
|
+
return { ok: true, entry: e, voice: e.voice, scope: e.scope, approved: e.approved };
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
module.exports = {
|
|
302
|
+
voicesHome,
|
|
303
|
+
listVoices,
|
|
304
|
+
resolveVoiceId,
|
|
305
|
+
pinOf,
|
|
306
|
+
approvalKey,
|
|
307
|
+
canonical,
|
|
308
|
+
readApprovals,
|
|
309
|
+
validateVoiceEntry,
|
|
310
|
+
KOKORO_V1_VOICES,
|
|
311
|
+
VOICE_ID_RE,
|
|
312
|
+
};
|