@gobing-ai/knowledge-kit 0.1.20 → 0.1.23
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/.claude-plugin/marketplace.json +1 -1
- package/dist/board/collect/index.js +1 -0
- package/dist/board/daily-voice/index.js +1279 -261
- package/dist/board/itc/index.js +1 -0
- package/dist/index.js +13938 -8966
- package/package.json +1 -1
- package/plugins/generations/dailynews-gen/dist/index.js +285 -16
- package/plugins/generations/dailynews-gen/src/editorial-claims.ts +215 -0
- package/plugins/generations/dailynews-gen/src/index.ts +23 -4
- package/plugins/generations/dailynews-gen/src/script-builder.ts +10 -3
- package/plugins/generations/dailynews-gen/src/script-plan.ts +74 -14
- package/plugins/generations/gemini-tts/dist/index.js +47 -7
- package/plugins/generations/gemini-tts/src/gemini-client.ts +34 -6
- package/plugins/generations/music-gen/README.md +277 -0
- package/plugins/generations/music-gen/dist/index.js +23580 -0
- package/plugins/generations/music-gen/package.json +24 -0
- package/plugins/generations/music-gen/plugin.json +23 -0
- package/plugins/generations/music-gen/presets/SOURCE.md +39 -0
- package/plugins/generations/music-gen/presets/formats/clip.yaml +21 -0
- package/plugins/generations/music-gen/presets/formats/jingle.yaml +22 -0
- package/plugins/generations/music-gen/presets/formats/loop.yaml +21 -0
- package/plugins/generations/music-gen/presets/formats/song.yaml +36 -0
- package/plugins/generations/music-gen/presets/formats/underscore.yaml +22 -0
- package/plugins/generations/music-gen/presets/themes/ambient-tech.yaml +30 -0
- package/plugins/generations/music-gen/presets/themes/cinematic.yaml +30 -0
- package/plugins/generations/music-gen/presets/themes/lofi.yaml +30 -0
- package/plugins/generations/music-gen/presets/themes/synthwave.yaml +35 -0
- package/plugins/generations/music-gen/scripts/gen-preset-index.ts +181 -0
- package/plugins/generations/music-gen/scripts/resolve-preset.ts +64 -0
- package/plugins/generations/music-gen/src/adapter/index.ts +35 -0
- package/plugins/generations/music-gen/src/adapter/instance.ts +927 -0
- package/plugins/generations/music-gen/src/adapter/offline.ts +124 -0
- package/plugins/generations/music-gen/src/artifact.ts +223 -0
- package/plugins/generations/music-gen/src/audio/checks.ts +618 -0
- package/plugins/generations/music-gen/src/audio/master.ts +267 -0
- package/plugins/generations/music-gen/src/audio/measure.ts +507 -0
- package/plugins/generations/music-gen/src/audio/profile.ts +131 -0
- package/plugins/generations/music-gen/src/audio.ts +122 -0
- package/plugins/generations/music-gen/src/brief.ts +191 -0
- package/plugins/generations/music-gen/src/config.ts +451 -0
- package/plugins/generations/music-gen/src/content.ts +199 -0
- package/plugins/generations/music-gen/src/digest.ts +100 -0
- package/plugins/generations/music-gen/src/index.ts +147 -0
- package/plugins/generations/music-gen/src/job.ts +131 -0
- package/plugins/generations/music-gen/src/plugin.ts +371 -0
- package/plugins/generations/music-gen/src/presets.ts +951 -0
- package/plugins/generations/music-gen/src/render.ts +87 -0
- package/plugins/generations/music-gen/src/request.ts +102 -0
- package/plugins/generations/music-gen/src/spend.ts +495 -0
- package/plugins/generations/music-gen/tsconfig.json +8 -0
- package/plugins/generations/omni-voice-gen/uv.toml +5 -0
- package/plugins/generations/vibe-voice-tts/README.md +64 -0
- package/plugins/generations/vibe-voice-tts/dist/index.js +14259 -0
- package/plugins/generations/vibe-voice-tts/package.json +16 -0
- package/plugins/generations/vibe-voice-tts/plugin.json +6 -0
- package/plugins/generations/vibe-voice-tts/profiles.json +10 -0
- package/plugins/generations/vibe-voice-tts/pyproject.toml +9 -0
- package/plugins/generations/vibe-voice-tts/python/worker.py +566 -0
- package/plugins/generations/vibe-voice-tts/src/index.ts +88 -0
- package/plugins/generations/vibe-voice-tts/src/vibevoice-client.ts +828 -0
- package/plugins/generations/vibe-voice-tts/tsconfig.json +8 -0
- package/plugins/generations/vibe-voice-tts/uv.lock +1767 -0
- package/plugins/generations/vibe-voice-tts/uv.toml +4 -0
- package/plugins/generations/voice-gen/dist/index.js +31 -1
- package/plugins/generations/voice-gen/src/voicebox-client.ts +50 -1
- package/plugins/generations/voxcpm-tts/dist/index.js +81 -9
- package/plugins/generations/voxcpm-tts/python/worker.py +9 -0
- package/plugins/generations/voxcpm-tts/src/index.ts +41 -0
- package/plugins/generations/voxcpm-tts/src/voxcpm-client.ts +32 -11
- package/plugins/generations/voxcpm-tts/uv.toml +5 -0
- package/plugins/kk/README.md +21 -0
- package/plugins/kk/decisions/kk-safety.yaml +5 -2
- package/plugins/kk/plugin.json +1 -1
- package/plugins/kk/schedules/kk-serve.launchd.plist.tmpl +14 -2
- package/plugins/kk/scripts/cover-compositor.ts +380 -32
- package/plugins/kk/scripts/daily-cover-mock.ts +40 -3
- package/plugins/kk/scripts/daily-stages.ts +933 -60
- package/plugins/kk/scripts/episode-identity.ts +180 -0
- package/plugins/kk/scripts/kk-workflow-stages.ts +111 -12
- package/plugins/kk/scripts/music-stages.ts +624 -0
- package/plugins/kk/scripts/news-editorial-ledger.ts +906 -0
- package/plugins/kk/scripts/safety-artifact-check.ts +28 -5
- package/plugins/kk/scripts/source-citation.ts +187 -0
- package/plugins/kk/scripts/topic-selection-attempts.ts +446 -0
- package/plugins/kk/scripts/topic-selection-calibration.ts +161 -0
- package/plugins/kk/skills/content-judge/SKILL.md +11 -6
- package/plugins/kk/skills/content-judge/references/music-quality-rubric.md +147 -0
- package/plugins/kk/skills/content-judge/references/rubrics.md +15 -0
- package/plugins/kk/skills/content-safety/SKILL.md +12 -0
- package/plugins/kk/skills/music-authoring/SKILL.md +276 -0
- package/plugins/kk/skills/music-authoring/references/judge-input.md +117 -0
- package/plugins/kk/skills/news-editorial/SKILL.md +102 -15
- package/plugins/kk/skills/news-editorial/references/selection.md +4 -0
- package/plugins/kk/skills/news-editorial/references/style-contract.zh.md +58 -11
- package/plugins/kk/workflows/kk-daily-ai-voice.yaml +12 -11
- package/plugins/kk/workflows/kk-daily-ai-voice2.yaml +13 -12
- package/plugins/kk/workflows/kk-daily-safety.yaml +12 -10
- package/plugins/kk/workflows/kk-music.yaml +332 -0
- package/plugins/kk/workflows/kk-news-generate.yaml +81 -6
- package/plugins/kk/workflows/kk-publish.yaml +4 -1
- package/plugins/publishings/bilibili-pub/dist/index.js +11 -1
- package/plugins/publishings/bilibili-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/cnblogs-pub/dist/index.js +11 -1
- package/plugins/publishings/cnblogs-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/csdn-pub/dist/index.js +11 -1
- package/plugins/publishings/csdn-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/infoq-pub/dist/index.js +11 -1
- package/plugins/publishings/infoq-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/jike-pub/dist/index.js +11 -1
- package/plugins/publishings/jike-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/juejin-pub/dist/index.js +11 -1
- package/plugins/publishings/juejin-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/linkedin-pub/dist/index.js +62 -10
- package/plugins/publishings/linkedin-pub/dist/profile-check.js +16 -1
- package/plugins/publishings/linkedin-pub/src/index.ts +99 -12
- package/plugins/publishings/podcast-pub/dist/index.js +586 -20
- package/plugins/publishings/podcast-pub/src/corrections.ts +509 -0
- package/plugins/publishings/podcast-pub/src/index.ts +257 -5
- package/plugins/publishings/podcast-pub/src/show-notes-brief.ts +240 -0
- package/plugins/publishings/podcast-pub/src/show-notes.ts +69 -13
- package/plugins/publishings/shipinhao-pub/dist/index.js +59 -8
- package/plugins/publishings/shipinhao-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/shipinhao-pub/src/audio-flow.ts +105 -6
- package/plugins/publishings/sspai-pub/dist/index.js +11 -1
- package/plugins/publishings/sspai-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/substack-pub/dist/index.js +11 -1
- package/plugins/publishings/substack-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/surfdash-pub/dist/index.js +80 -24
- package/plugins/publishings/surfdash-pub/src/digest-check.ts +7 -3
- package/plugins/publishings/surfdash-pub/src/index.ts +107 -18
- package/plugins/publishings/surfdash-pub/src/patch.ts +21 -3
- package/plugins/publishings/surfdash-pub/src/topic-claims.ts +3 -1
- package/plugins/publishings/threads-pub/dist/index.js +58 -6
- package/plugins/publishings/threads-pub/dist/profile-check.js +18 -2
- package/plugins/publishings/threads-pub/src/post-rail.ts +101 -4
- package/plugins/publishings/wechatmp-pub/dist/index.js +105 -35
- package/plugins/publishings/wechatmp-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/wechatmp-pub/src/cgi-draft.ts +87 -53
- package/plugins/publishings/wechatmp-pub/src/enrich.ts +61 -3
- package/plugins/publishings/wechatmp-pub/src/flow.ts +28 -0
- package/plugins/publishings/weibo-pub/dist/index.js +11 -1
- package/plugins/publishings/weibo-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/x-pub/dist/index.js +95 -10
- package/plugins/publishings/x-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/x-pub/src/post-rail.ts +154 -12
- package/plugins/publishings/xhs-pub/dist/index.js +11 -1
- package/plugins/publishings/xhs-pub/dist/profile-check.js +11 -1
- package/plugins/publishings/xiaoyuzhou-pub/dist/index.js +11 -1
- package/plugins/publishings/xiaoyuzhou-pub/dist/profile-check.js +11 -1
|
@@ -0,0 +1,509 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 0396 R1/R2 — traceable public corrections (L3 R17: 已播纠错随新版节目送达并可追溯).
|
|
3
|
+
*
|
|
4
|
+
* A correction is *not* a retro-edit of a published episode. It is content carried by the NEXT
|
|
5
|
+
* eligible episode, and it is traceable in two directions:
|
|
6
|
+
*
|
|
7
|
+
* - **into the content** — `renderCorrectionsBlock` turns the frozen judgment snapshot's
|
|
8
|
+
* `corrections[]` (0384: stable `corr-<digest>` ids, the retired record, the replacement wording,
|
|
9
|
+
* the evidence) into the acknowledgement + apology + corrected fact block, naming the prior
|
|
10
|
+
* episode the wrong judgment aired in. No frozen correction list ⇒ `null`, so a caller can never
|
|
11
|
+
* emit an empty heading.
|
|
12
|
+
* - **out of the delivery** — `classifyCorrectionTarget` / `recordCorrectionDeliveries` produce one
|
|
13
|
+
* delivery record per target (`{correctionId, target, state, reason}`), naming the 0388 target and
|
|
14
|
+
* the correction id. A target that is manual, blocked or unsupported gets an EXACT target-level
|
|
15
|
+
* reason verbatim (never a generic failure) and is never recorded as `delivered`.
|
|
16
|
+
*
|
|
17
|
+
* Reuse, not a second transport: the records are plain data written beside the run's publish receipts
|
|
18
|
+
* (`correctionDeliveryPath`), and the anchor for an update of an already-published item stays
|
|
19
|
+
* podcast-pub's existing `update` operation. Nothing here calls an API.
|
|
20
|
+
*/
|
|
21
|
+
import { join } from 'node:path';
|
|
22
|
+
import { ensureDirForFile, readJsonFile, writeJsonFile } from '@gobing-ai/ts-runtime';
|
|
23
|
+
|
|
24
|
+
/** The evidence a correction rests on — carried verbatim from the frozen judgment. */
|
|
25
|
+
export interface CorrectionEvidence {
|
|
26
|
+
url: string;
|
|
27
|
+
checkedAt: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** The retired judgment as committed: the correction's subject, answerable without the ledger. */
|
|
31
|
+
export interface RetiredJudgment {
|
|
32
|
+
id: string;
|
|
33
|
+
/** The (prior) episode date the wrong judgment aired on — the "prior episode" reference. */
|
|
34
|
+
date: string;
|
|
35
|
+
fact: string;
|
|
36
|
+
attribution: string | null;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** One frozen correction: what was wrong, what replaces it, and the stable id that names it. */
|
|
40
|
+
export interface Correction {
|
|
41
|
+
/** `corr-<digest>` from 0384 — stable across replays, the dedupe key. */
|
|
42
|
+
correctionId: string;
|
|
43
|
+
supersedes: string;
|
|
44
|
+
judgmentId: string;
|
|
45
|
+
selectedId: string | null;
|
|
46
|
+
/** The replacement wording the episode speaks. */
|
|
47
|
+
wording: string;
|
|
48
|
+
/** The renderable correction sentence frozen with the snapshot. */
|
|
49
|
+
text: string;
|
|
50
|
+
retired: RetiredJudgment;
|
|
51
|
+
evidence: CorrectionEvidence;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The validated read of a frozen snapshot's correction projection. */
|
|
55
|
+
export interface FrozenCorrectionSet {
|
|
56
|
+
runDate: string | null;
|
|
57
|
+
corrections: Correction[];
|
|
58
|
+
/** `## 本期更正说明` preamble frozen by 0384 (null when the run owed none). */
|
|
59
|
+
preamble: string | null;
|
|
60
|
+
/** Malformed / duplicate entries — reported, never silently dropped. */
|
|
61
|
+
diagnostics: string[];
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const EMPTY: Correction[] = [];
|
|
65
|
+
const NO_DELIVERIES: CorrectionDelivery[] = [];
|
|
66
|
+
|
|
67
|
+
function asRecord(value: unknown): Record<string, unknown> | null {
|
|
68
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value)
|
|
69
|
+
? (value as Record<string, unknown>)
|
|
70
|
+
: null;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function asString(value: unknown): string {
|
|
74
|
+
return typeof value === 'string' ? value.trim() : '';
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** `第261118期(2026-11-18)` — the issue label the correction names as the prior episode. */
|
|
78
|
+
export function priorEpisodeReference(retired: RetiredJudgment): string {
|
|
79
|
+
const digits = retired.date.replaceAll(/\D/g, '');
|
|
80
|
+
if (digits.length < 8) return `该期(${retired.date.trim()})`;
|
|
81
|
+
const day = digits.slice(0, 8);
|
|
82
|
+
const iso = `${day.slice(0, 4)}-${day.slice(4, 6)}-${day.slice(6, 8)}`;
|
|
83
|
+
return `第${day.slice(-6)}期(${iso})`;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** Validates one snapshot correction entry; `null` (with a diagnostic reason) when unusable. */
|
|
87
|
+
function readCorrection(raw: unknown, index: number): { correction: Correction } | { reason: string } {
|
|
88
|
+
const entry = asRecord(raw);
|
|
89
|
+
if (entry === null) return { reason: `corrections[${index}] is not an object` };
|
|
90
|
+
const correctionId = asString(entry.correctionId);
|
|
91
|
+
if (correctionId === '') return { reason: `corrections[${index}] has no correctionId` };
|
|
92
|
+
const retiredRaw = asRecord(entry.retired);
|
|
93
|
+
if (retiredRaw === null) return { reason: `corrections[${index}] (${correctionId}) has no retired record` };
|
|
94
|
+
const date = asString(retiredRaw.date);
|
|
95
|
+
const fact = asString(retiredRaw.fact);
|
|
96
|
+
if (date === '' || fact === '') {
|
|
97
|
+
return { reason: `corrections[${index}] (${correctionId}) has no retired date/fact to name the prior episode` };
|
|
98
|
+
}
|
|
99
|
+
const evidenceRaw = asRecord(entry.evidence);
|
|
100
|
+
const wording = asString(entry.wording);
|
|
101
|
+
const text = asString(entry.text);
|
|
102
|
+
return {
|
|
103
|
+
correction: {
|
|
104
|
+
correctionId,
|
|
105
|
+
supersedes: asString(entry.supersedes) || asString(retiredRaw.id),
|
|
106
|
+
judgmentId: asString(entry.judgmentId),
|
|
107
|
+
selectedId: asString(entry.selectedId) || null,
|
|
108
|
+
wording,
|
|
109
|
+
text: text || `「${fact}」经核验有误,现更正为:${wording || '(新判断)'}`,
|
|
110
|
+
retired: {
|
|
111
|
+
id: asString(retiredRaw.id),
|
|
112
|
+
date,
|
|
113
|
+
fact,
|
|
114
|
+
attribution: asString(retiredRaw.attribution) || null,
|
|
115
|
+
},
|
|
116
|
+
evidence: {
|
|
117
|
+
url: asString(evidenceRaw?.url),
|
|
118
|
+
checkedAt: asString(evidenceRaw?.checkedAt) || null,
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Read the frozen snapshot's correction projection (0384 `21-plan/<date>_judgment-snapshot.json`).
|
|
126
|
+
* Tolerant by contract: a missing/odd shape yields zero corrections plus a diagnostic — an absent
|
|
127
|
+
* frozen judgment means *no correction block*, never a fabricated one and never a generic crash.
|
|
128
|
+
* Correction ids are unique: a repeated id is ignored (the first wins) and reported.
|
|
129
|
+
*/
|
|
130
|
+
export function readFrozenCorrections(raw: unknown, options: { runDate?: string | null } = {}): FrozenCorrectionSet {
|
|
131
|
+
const runDateFromOptions = options.runDate ?? null;
|
|
132
|
+
const snapshot = asRecord(raw);
|
|
133
|
+
if (snapshot === null) {
|
|
134
|
+
return {
|
|
135
|
+
runDate: runDateFromOptions,
|
|
136
|
+
corrections: EMPTY,
|
|
137
|
+
preamble: null,
|
|
138
|
+
diagnostics: ['correction snapshot is not an object: no corrections carried'],
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
const runDate = asString(snapshot.runDate) || runDateFromOptions;
|
|
142
|
+
const rawCorrections = Array.isArray(snapshot.corrections) ? snapshot.corrections : [];
|
|
143
|
+
const corrections: Correction[] = [];
|
|
144
|
+
const diagnostics: string[] = [];
|
|
145
|
+
const seen = new Set<string>();
|
|
146
|
+
for (const [index, entry] of rawCorrections.entries()) {
|
|
147
|
+
const parsed = readCorrection(entry, index);
|
|
148
|
+
if ('reason' in parsed) {
|
|
149
|
+
diagnostics.push(parsed.reason);
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
const { correctionId } = parsed.correction;
|
|
153
|
+
if (seen.has(correctionId)) {
|
|
154
|
+
diagnostics.push(`duplicate correction id "${correctionId}" at corrections[${index}] ignored`);
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
seen.add(correctionId);
|
|
158
|
+
corrections.push(parsed.correction);
|
|
159
|
+
}
|
|
160
|
+
return {
|
|
161
|
+
runDate,
|
|
162
|
+
corrections,
|
|
163
|
+
preamble: asString(snapshot.preamble) || null,
|
|
164
|
+
diagnostics,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** `<!--kk:corrections-->` — the marker the show-notes renderer trusts (one block, one place). */
|
|
169
|
+
export const CORRECTIONS_TAG = '<!--kk:corrections-->';
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* The public correction block: acknowledgement, apology, corrected fact, prior-episode reference and
|
|
173
|
+
* the evidence. Returns `null` when the run owes no correction (no frozen judgment ⇒ no block and no
|
|
174
|
+
* empty heading). Each entry is a flat, closed bullet list so the RSS/plain-text degradation keeps it
|
|
175
|
+
* readable, and every line is escaped for markdown-unsafe characters.
|
|
176
|
+
*/
|
|
177
|
+
export function renderCorrectionsBlock(set: FrozenCorrectionSet): string | null {
|
|
178
|
+
if (set.corrections.length === 0) return null;
|
|
179
|
+
const lines: string[] = ['## 本期更正说明', '', CORRECTIONS_TAG, ''];
|
|
180
|
+
if (set.preamble !== null && set.preamble !== '') lines.push(set.preamble, '');
|
|
181
|
+
lines.push('此前节目中的一处判断经核验有误,我们在此更正并致歉。', '');
|
|
182
|
+
for (const correction of set.corrections) {
|
|
183
|
+
lines.push(
|
|
184
|
+
`- **更正 ${correction.correctionId}** · 上期节目 ${priorEpisodeReference(correction.retired)}`,
|
|
185
|
+
` - 原判断:「${correction.retired.fact}」`,
|
|
186
|
+
` - 更正为:${correction.wording !== '' ? correction.wording : correction.text}`,
|
|
187
|
+
` - ${evidenceLine(correction)}`,
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
return `${lines.join('\n')}\n`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function evidenceLine(correction: Correction): string {
|
|
194
|
+
const { url, checkedAt } = correction.evidence;
|
|
195
|
+
if (url === '') return `依据:判断记录 ${correction.retired.id || correction.supersedes}`;
|
|
196
|
+
return `依据:${url}${checkedAt === null ? '' : `(核验于 ${checkedAt})`}`;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** Where a correction stands for one target. Only `delivered` means it reached the audience. */
|
|
200
|
+
export type CorrectionTargetState = 'delivered' | 'pending' | 'manual' | 'blocked' | 'unsupported';
|
|
201
|
+
|
|
202
|
+
/** One 0388 target as the correction delivery sees it (dispatch ledger row + capability facts). */
|
|
203
|
+
export interface CorrectionDeliveryTarget {
|
|
204
|
+
/** 0388 dispatch id, e.g. `podcast-pub` / `surfdash-pub@article`. */
|
|
205
|
+
target: string;
|
|
206
|
+
mode?: string;
|
|
207
|
+
/** 0388 `DispatchState`: dispatched | pending | retry | blocked | deferred | off. */
|
|
208
|
+
state?: string;
|
|
209
|
+
manualStep?: string;
|
|
210
|
+
blockedBy?: readonly string[];
|
|
211
|
+
lastError?: string;
|
|
212
|
+
/**
|
|
213
|
+
* Explicit capability: does this adapter carry episode content and support updating it? A target
|
|
214
|
+
* without both is `unsupported` — its reason names the adapter gap, never "delivery failed".
|
|
215
|
+
*/
|
|
216
|
+
supportsCorrections: boolean;
|
|
217
|
+
/** The adapter-specific capability gap, when `supportsCorrections` is false. */
|
|
218
|
+
capabilityReason?: string;
|
|
219
|
+
/**
|
|
220
|
+
* Evidence that THIS target's approved content actually carries the correction block (the
|
|
221
|
+
* published item's url or content id). A dispatched non-podcast target without it is `blocked`:
|
|
222
|
+
* the ledger must not claim a correction travelled somewhere nobody looked (0396 review P3).
|
|
223
|
+
*/
|
|
224
|
+
deliveryEvidence?: string;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** One correction's delivery outcome on one target — the traceable record. */
|
|
228
|
+
export interface CorrectionDelivery {
|
|
229
|
+
correctionId: string;
|
|
230
|
+
target: string;
|
|
231
|
+
runDate: string | null;
|
|
232
|
+
state: CorrectionTargetState;
|
|
233
|
+
/** EXACT reason, always present, always naming the target and the correction id. */
|
|
234
|
+
reason: string;
|
|
235
|
+
deliveredAt: string | null;
|
|
236
|
+
url?: string;
|
|
237
|
+
itemId?: string;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The durable delivery ledger (beside the run's publish receipts). */
|
|
241
|
+
export interface CorrectionDeliveryLedger {
|
|
242
|
+
schemaVersion: 1;
|
|
243
|
+
runDate: string | null;
|
|
244
|
+
deliveries: CorrectionDelivery[];
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const DELIVERED_KEY = (delivery: { correctionId: string; target: string }): string =>
|
|
248
|
+
`${delivery.correctionId}\u0000${delivery.target}`;
|
|
249
|
+
|
|
250
|
+
function delivery(
|
|
251
|
+
correction: Correction,
|
|
252
|
+
target: CorrectionDeliveryTarget,
|
|
253
|
+
runDate: string | null,
|
|
254
|
+
state: CorrectionTargetState,
|
|
255
|
+
reason: string,
|
|
256
|
+
deliveredAt: string | null,
|
|
257
|
+
url?: string,
|
|
258
|
+
): CorrectionDelivery {
|
|
259
|
+
return {
|
|
260
|
+
correctionId: correction.correctionId,
|
|
261
|
+
target: target.target,
|
|
262
|
+
runDate,
|
|
263
|
+
state,
|
|
264
|
+
reason,
|
|
265
|
+
deliveredAt: state === 'delivered' ? deliveredAt : null,
|
|
266
|
+
...(state === 'delivered' && url !== undefined && url !== '' ? { url } : {}),
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* The target whose delivery this module can verify itself: the podcast item, anchored by the item id
|
|
272
|
+
* and public URL the caller just produced. Any other target must supply `deliveryEvidence`.
|
|
273
|
+
*/
|
|
274
|
+
function isSelfTarget(target: string): boolean {
|
|
275
|
+
return target === SELF_TARGET;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/** The podcast row the plugin itself dispatches (`TARGET` in index.ts is 'podcast-pub'). */
|
|
279
|
+
const SELF_TARGET = 'podcast-pub';
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Classify one correction against one target. Order matters: a duplicate (replay) is recognised
|
|
283
|
+
* before anything else so a retried trigger never re-sends, and an authorization `off` outranks the
|
|
284
|
+
* dispatch state (an off target is off even if a stale ledger says `pending`).
|
|
285
|
+
*/
|
|
286
|
+
export function classifyCorrectionTarget(
|
|
287
|
+
correction: Correction,
|
|
288
|
+
target: CorrectionDeliveryTarget,
|
|
289
|
+
options: { runDate?: string | null; prior?: readonly CorrectionDelivery[]; now?: string } = {},
|
|
290
|
+
): CorrectionDelivery {
|
|
291
|
+
const runDate = options.runDate ?? null;
|
|
292
|
+
const now = options.now ?? new Date().toISOString();
|
|
293
|
+
const id = correction.correctionId;
|
|
294
|
+
const prior = (options.prior ?? NO_DELIVERIES).find(
|
|
295
|
+
(entry) => entry.correctionId === id && entry.target === target.target && entry.state === 'delivered',
|
|
296
|
+
);
|
|
297
|
+
if (prior !== undefined) {
|
|
298
|
+
return delivery(
|
|
299
|
+
correction,
|
|
300
|
+
target,
|
|
301
|
+
runDate,
|
|
302
|
+
'delivered',
|
|
303
|
+
`target "${target.target}" already received correction ${id} (delivered ${prior.deliveredAt ?? 'earlier'}); not re-sent`,
|
|
304
|
+
prior.deliveredAt ?? now,
|
|
305
|
+
);
|
|
306
|
+
}
|
|
307
|
+
if (target.mode === 'off') {
|
|
308
|
+
return delivery(
|
|
309
|
+
correction,
|
|
310
|
+
target,
|
|
311
|
+
runDate,
|
|
312
|
+
'unsupported',
|
|
313
|
+
`target "${target.target}" is off in the authorization matrix (mode=off): correction ${id} cannot be delivered there`,
|
|
314
|
+
now,
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
if (target.manualStep !== undefined && target.manualStep !== '') {
|
|
318
|
+
return delivery(
|
|
319
|
+
correction,
|
|
320
|
+
target,
|
|
321
|
+
runDate,
|
|
322
|
+
'manual',
|
|
323
|
+
`target "${target.target}" requires the manual step "${target.manualStep}": correction ${id} stays a pending operator step until it is confirmed`,
|
|
324
|
+
now,
|
|
325
|
+
);
|
|
326
|
+
}
|
|
327
|
+
if (target.state === 'blocked') {
|
|
328
|
+
const blockers = (target.blockedBy ?? []).join(', ');
|
|
329
|
+
return delivery(
|
|
330
|
+
correction,
|
|
331
|
+
target,
|
|
332
|
+
runDate,
|
|
333
|
+
'blocked',
|
|
334
|
+
`target "${target.target}" is blocked by unsatisfied dependenc${(target.blockedBy ?? []).length === 1 ? 'y' : 'ies'} ${blockers === '' ? '(unspecified)' : blockers}: correction ${id} is not delivered yet`,
|
|
335
|
+
now,
|
|
336
|
+
);
|
|
337
|
+
}
|
|
338
|
+
if (target.state === 'retry') {
|
|
339
|
+
return delivery(
|
|
340
|
+
correction,
|
|
341
|
+
target,
|
|
342
|
+
runDate,
|
|
343
|
+
'blocked',
|
|
344
|
+
`target "${target.target}" failed its last dispatch (${target.lastError ?? 'dispatch failed'}): correction ${id} is not delivered`,
|
|
345
|
+
now,
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
if (target.state === 'deferred') {
|
|
349
|
+
return delivery(
|
|
350
|
+
correction,
|
|
351
|
+
target,
|
|
352
|
+
runDate,
|
|
353
|
+
'pending',
|
|
354
|
+
`target "${target.target}" dispatches in its own, later window: correction ${id} is carried when that window runs`,
|
|
355
|
+
now,
|
|
356
|
+
);
|
|
357
|
+
}
|
|
358
|
+
if (!target.supportsCorrections) {
|
|
359
|
+
return delivery(
|
|
360
|
+
correction,
|
|
361
|
+
target,
|
|
362
|
+
runDate,
|
|
363
|
+
'unsupported',
|
|
364
|
+
`target "${target.target}" cannot carry an episode correction (${target.capabilityReason ?? 'no correction-capable content surface with same-item update'}): correction ${id} is unsupported there`,
|
|
365
|
+
now,
|
|
366
|
+
);
|
|
367
|
+
}
|
|
368
|
+
if (target.state === 'dispatched') {
|
|
369
|
+
const evidence = target.deliveryEvidence?.trim() ?? '';
|
|
370
|
+
if (isSelfTarget(target.target) || evidence !== '') {
|
|
371
|
+
return delivery(
|
|
372
|
+
correction,
|
|
373
|
+
target,
|
|
374
|
+
runDate,
|
|
375
|
+
'delivered',
|
|
376
|
+
`target "${target.target}" dispatched${runDate === null ? '' : ` on ${runDate}`} with the episode: correction ${id} travelled in its approved content${evidence === '' ? '' : ` (evidence: ${evidence})`}`,
|
|
377
|
+
now,
|
|
378
|
+
evidence === '' ? undefined : evidence,
|
|
379
|
+
);
|
|
380
|
+
}
|
|
381
|
+
return delivery(
|
|
382
|
+
correction,
|
|
383
|
+
target,
|
|
384
|
+
runDate,
|
|
385
|
+
'blocked',
|
|
386
|
+
`target "${target.target}" dispatched${runDate === null ? '' : ` on ${runDate}`} but carries no delivery evidence for correction ${id}: the block's presence in that content is unverified, so the correction is not recorded as delivered`,
|
|
387
|
+
now,
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
return delivery(
|
|
391
|
+
correction,
|
|
392
|
+
target,
|
|
393
|
+
runDate,
|
|
394
|
+
'pending',
|
|
395
|
+
`target "${target.target}" has not dispatched yet: correction ${id} is queued with the next eligible episode`,
|
|
396
|
+
now,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Classify every frozen correction against every target (the per-target delivery plan). */
|
|
401
|
+
export function planCorrectionDeliveries(
|
|
402
|
+
set: FrozenCorrectionSet,
|
|
403
|
+
targets: readonly CorrectionDeliveryTarget[],
|
|
404
|
+
options: { runDate?: string | null; prior?: readonly CorrectionDelivery[]; now?: string } = {},
|
|
405
|
+
): CorrectionDelivery[] {
|
|
406
|
+
const deliveries: CorrectionDelivery[] = [];
|
|
407
|
+
for (const correction of set.corrections) {
|
|
408
|
+
for (const target of targets) {
|
|
409
|
+
deliveries.push(classifyCorrectionTarget(correction, target, options));
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return deliveries;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* Fold deliveries into the ledger. `delivered` is the only state that is recorded as a delivery and
|
|
417
|
+
* the only one deduped per `(correctionId, target)` pair: a correction already delivered is never
|
|
418
|
+
* re-delivered (the repeat is returned in `skipped`), and a later non-delivered pass can never
|
|
419
|
+
* downgrade a delivered pair — the record of a real delivery is immutable.
|
|
420
|
+
*/
|
|
421
|
+
export function recordCorrectionDeliveries(
|
|
422
|
+
ledger: CorrectionDeliveryLedger,
|
|
423
|
+
deliveries: readonly CorrectionDelivery[],
|
|
424
|
+
): { ledger: CorrectionDeliveryLedger; recorded: CorrectionDelivery[]; skipped: CorrectionDelivery[] } {
|
|
425
|
+
const kept = [...ledger.deliveries];
|
|
426
|
+
const recorded: CorrectionDelivery[] = [];
|
|
427
|
+
const skipped: CorrectionDelivery[] = [];
|
|
428
|
+
const deliveredKeys = new Set(kept.filter((entry) => entry.state === 'delivered').map(DELIVERED_KEY));
|
|
429
|
+
for (const entry of deliveries) {
|
|
430
|
+
const key = DELIVERED_KEY(entry);
|
|
431
|
+
const index = kept.findIndex((existing) => DELIVERED_KEY(existing) === key);
|
|
432
|
+
if (entry.state === 'delivered') {
|
|
433
|
+
if (deliveredKeys.has(key)) {
|
|
434
|
+
skipped.push(entry);
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
deliveredKeys.add(key);
|
|
438
|
+
recorded.push(entry);
|
|
439
|
+
if (index === -1) kept.push(entry);
|
|
440
|
+
else kept[index] = entry;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
recorded.push(entry);
|
|
444
|
+
if (index === -1) kept.push(entry);
|
|
445
|
+
else if (kept[index]?.state !== 'delivered') kept[index] = entry;
|
|
446
|
+
}
|
|
447
|
+
return { ledger: { ...ledger, deliveries: kept }, recorded, skipped };
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
/** The correction ids a ledger proves delivered (optionally narrowed to one target). */
|
|
451
|
+
export function deliveredCorrectionIds(ledger: CorrectionDeliveryLedger, target?: string): string[] {
|
|
452
|
+
const ids = new Set<string>();
|
|
453
|
+
for (const entry of ledger.deliveries) {
|
|
454
|
+
if (entry.state !== 'delivered') continue;
|
|
455
|
+
if (target !== undefined && entry.target !== target) continue;
|
|
456
|
+
ids.add(entry.correctionId);
|
|
457
|
+
}
|
|
458
|
+
return [...ids];
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** An empty ledger for `runDate`. */
|
|
462
|
+
export function emptyDeliveryLedger(runDate: string | null): CorrectionDeliveryLedger {
|
|
463
|
+
return { schemaVersion: 1, runDate, deliveries: [] };
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
/** Absolute path of a run's correction delivery ledger: `31-publish/<date>_16_corrections-delivery.json`. */
|
|
467
|
+
export function correctionDeliveryPath(workDir: string, runDate: string): string {
|
|
468
|
+
return join(workDir, '31-publish', `${runDate}_16_corrections-delivery.json`);
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
/**
|
|
472
|
+
* Read a delivery ledger. An absent or corrupt file is an EMPTY ledger, never an error: an unreadable
|
|
473
|
+
* record cannot prove a delivery, so the next pass re-classifies from the dispatch state (it does not
|
|
474
|
+
* re-send a delivered item — the 0388 ledger still says `dispatched`).
|
|
475
|
+
*/
|
|
476
|
+
export async function readCorrectionDeliveries(
|
|
477
|
+
path: string,
|
|
478
|
+
runDate: string | null = null,
|
|
479
|
+
): Promise<CorrectionDeliveryLedger> {
|
|
480
|
+
try {
|
|
481
|
+
// The ts-runtime JSON seam (never `node:fs`): an absent file throws here and lands in the same
|
|
482
|
+
// `catch`, which is the documented "unreadable record proves nothing" path.
|
|
483
|
+
const parsed: unknown = await readJsonFile(path);
|
|
484
|
+
const record = asRecord(parsed);
|
|
485
|
+
const deliveries = Array.isArray(record?.deliveries) ? record.deliveries : [];
|
|
486
|
+
return {
|
|
487
|
+
schemaVersion: 1,
|
|
488
|
+
runDate: asString(record?.runDate) || runDate,
|
|
489
|
+
deliveries: deliveries.filter((entry): entry is CorrectionDelivery => {
|
|
490
|
+
const candidate = asRecord(entry);
|
|
491
|
+
return (
|
|
492
|
+
candidate !== null &&
|
|
493
|
+
asString(candidate.correctionId) !== '' &&
|
|
494
|
+
asString(candidate.target) !== '' &&
|
|
495
|
+
['delivered', 'pending', 'manual', 'blocked', 'unsupported'].includes(asString(candidate.state))
|
|
496
|
+
);
|
|
497
|
+
}),
|
|
498
|
+
};
|
|
499
|
+
} catch {
|
|
500
|
+
return emptyDeliveryLedger(runDate);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/** Persist the delivery ledger (durable evidence of where each correction was delivered). */
|
|
505
|
+
export async function writeCorrectionDeliveries(path: string, ledger: CorrectionDeliveryLedger): Promise<string> {
|
|
506
|
+
await ensureDirForFile(path);
|
|
507
|
+
await writeJsonFile(path, ledger);
|
|
508
|
+
return path;
|
|
509
|
+
}
|