@growth-labs/cms 0.8.2 → 0.8.3
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/dist/engine/ai-dek-notifications.d.ts +2 -0
- package/dist/engine/ai-dek-notifications.d.ts.map +1 -1
- package/dist/engine/ai-dek-notifications.js +11 -2
- package/dist/engine/ai-dek-notifications.js.map +1 -1
- package/dist/engine/ai-dek.d.ts +23 -0
- package/dist/engine/ai-dek.d.ts.map +1 -1
- package/dist/engine/ai-dek.js +94 -31
- package/dist/engine/ai-dek.js.map +1 -1
- package/dist/engine/foundry-dispatch.d.ts +13 -3
- package/dist/engine/foundry-dispatch.d.ts.map +1 -1
- package/dist/engine/foundry-dispatch.js +80 -25
- package/dist/engine/foundry-dispatch.js.map +1 -1
- package/dist/engine/fronts-publish-intent.d.ts +55 -0
- package/dist/engine/fronts-publish-intent.d.ts.map +1 -1
- package/dist/engine/fronts-publish-intent.js +181 -55
- package/dist/engine/fronts-publish-intent.js.map +1 -1
- package/dist/engine/fronts-publish.d.ts +11 -0
- package/dist/engine/fronts-publish.d.ts.map +1 -1
- package/dist/engine/fronts-publish.js +106 -4
- package/dist/engine/fronts-publish.js.map +1 -1
- package/dist/engine/index.d.ts +1 -1
- package/dist/engine/index.d.ts.map +1 -1
- package/dist/engine/index.js +1 -1
- package/dist/engine/index.js.map +1 -1
- package/dist/engine/podcast-source.d.ts +3 -2
- package/dist/engine/podcast-source.d.ts.map +1 -1
- package/dist/engine/podcast-source.js +6 -4
- package/dist/engine/podcast-source.js.map +1 -1
- package/dist/engine/publisher.d.ts.map +1 -1
- package/dist/engine/publisher.js +16 -8
- package/dist/engine/publisher.js.map +1 -1
- package/dist/engine/soft-delete.d.ts +3 -1
- package/dist/engine/soft-delete.d.ts.map +1 -1
- package/dist/engine/soft-delete.js +21 -5
- package/dist/engine/soft-delete.js.map +1 -1
- package/dist/engine/video-source.d.ts +4 -2
- package/dist/engine/video-source.d.ts.map +1 -1
- package/dist/engine/video-source.js +7 -4
- package/dist/engine/video-source.js.map +1 -1
- package/dist/routes/content.d.ts +14 -0
- package/dist/routes/content.d.ts.map +1 -1
- package/dist/routes/content.js +37 -24
- package/dist/routes/content.js.map +1 -1
- package/dist/routes/context.d.ts +6 -0
- package/dist/routes/context.d.ts.map +1 -1
- package/dist/routes/context.js.map +1 -1
- package/dist/routes/fronts-publish.d.ts +2 -0
- package/dist/routes/fronts-publish.d.ts.map +1 -1
- package/dist/routes/fronts-publish.js +178 -2
- package/dist/routes/fronts-publish.js.map +1 -1
- package/dist/ui/api/_content-config.d.ts.map +1 -1
- package/dist/ui/api/_content-config.js +7 -0
- package/dist/ui/api/_content-config.js.map +1 -1
- package/dist/ui/api/fronts/schedule.d.ts +2 -0
- package/dist/ui/api/fronts/schedule.d.ts.map +1 -1
- package/dist/ui/api/fronts/schedule.js +2 -0
- package/dist/ui/api/fronts/schedule.js.map +1 -1
- package/dist/ui/editor/ContentForm.d.ts.map +1 -1
- package/dist/ui/editor/ContentForm.js +2 -2
- package/dist/ui/editor/ContentForm.js.map +1 -1
- package/package.json +1 -1
- package/src/engine/ai-dek-notifications.ts +19 -2
- package/src/engine/ai-dek.ts +131 -40
- package/src/engine/foundry-dispatch.ts +110 -24
- package/src/engine/fronts-publish-intent.ts +289 -65
- package/src/engine/fronts-publish.ts +121 -4
- package/src/engine/index.ts +1 -0
- package/src/engine/podcast-source.ts +6 -4
- package/src/engine/publisher.ts +20 -8
- package/src/engine/soft-delete.ts +28 -8
- package/src/engine/video-source.ts +7 -4
- package/src/routes/content.ts +56 -31
- package/src/routes/context.ts +6 -0
- package/src/routes/fronts-publish.ts +213 -2
- package/src/ui/api/_content-config.ts +15 -0
- package/src/ui/api/fronts/schedule.ts +4 -0
- package/src/ui/editor/ContentForm.tsx +4 -5
package/src/engine/ai-dek.ts
CHANGED
|
@@ -2,6 +2,11 @@ import type { LlmDispatch } from '../providers/types.js'
|
|
|
2
2
|
import { dekPrompt } from './ai-prompts.js'
|
|
3
3
|
import { applyAiWrite, parseLockedFields } from './ai-writeback.js'
|
|
4
4
|
import type { D1Database } from './d1.js'
|
|
5
|
+
import {
|
|
6
|
+
CURRENT_PUBLICATION_GUARD,
|
|
7
|
+
currentPublicationSqlGuard,
|
|
8
|
+
snapshotRecord,
|
|
9
|
+
} from './publication-current-guard.js'
|
|
5
10
|
import { type ContentItemRecord, getContentItem } from './publisher.js'
|
|
6
11
|
|
|
7
12
|
export const DEK_EXEMPLAR_LIMIT = 6
|
|
@@ -18,6 +23,10 @@ export type GenerateContentDekResult =
|
|
|
18
23
|
| { status: 'unavailable'; dek: null; written: false; reason: string }
|
|
19
24
|
| { status: 'not_found'; dek: null; written: false; reason: 'not_found' }
|
|
20
25
|
|
|
26
|
+
export type GenerateContentDekSuggestionResult =
|
|
27
|
+
| { status: 'generated'; dek: string }
|
|
28
|
+
| { status: 'unavailable'; dek: null; reason: string }
|
|
29
|
+
|
|
21
30
|
async function loadTypeSource(db: D1Database, item: ContentItemRecord): Promise<string> {
|
|
22
31
|
if (item.type === 'video') {
|
|
23
32
|
const row = await db
|
|
@@ -47,16 +56,7 @@ async function loadTypeSource(db: D1Database, item: ContentItemRecord): Promise<
|
|
|
47
56
|
return (row?.body_markdown || row?.body_html || row?.subtitle || '').trim()
|
|
48
57
|
}
|
|
49
58
|
|
|
50
|
-
|
|
51
|
-
db: D1Database,
|
|
52
|
-
contentId: string,
|
|
53
|
-
): Promise<DekGenerationContext | null> {
|
|
54
|
-
const item = await getContentItem(db, contentId)
|
|
55
|
-
if (!item) return null
|
|
56
|
-
|
|
57
|
-
const typeSource = await loadTypeSource(db, item)
|
|
58
|
-
const description = item.description?.trim() ?? ''
|
|
59
|
-
const source = [description, typeSource].filter(Boolean).join('\n\n')
|
|
59
|
+
async function loadExemplars(db: D1Database, item: ContentItemRecord): Promise<string[]> {
|
|
60
60
|
const rows = await db
|
|
61
61
|
.prepare(
|
|
62
62
|
`SELECT title, excerpt
|
|
@@ -71,13 +71,44 @@ export async function loadDekGenerationContext(
|
|
|
71
71
|
id ASC
|
|
72
72
|
LIMIT ?`,
|
|
73
73
|
)
|
|
74
|
-
.bind(
|
|
74
|
+
.bind(item.id, item.type, DEK_EXEMPLAR_LIMIT)
|
|
75
75
|
.all<{ title: string; excerpt: string }>()
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
)
|
|
76
|
+
return (rows.results ?? []).map((row) => `Headline: ${row.title}\nDek: ${row.excerpt.trim()}`)
|
|
77
|
+
}
|
|
79
78
|
|
|
80
|
-
|
|
79
|
+
export async function loadDekGenerationContext(
|
|
80
|
+
db: D1Database,
|
|
81
|
+
contentId: string,
|
|
82
|
+
): Promise<DekGenerationContext | null> {
|
|
83
|
+
const item = await getContentItem(db, contentId)
|
|
84
|
+
if (!item) return null
|
|
85
|
+
|
|
86
|
+
const typeSource = await loadTypeSource(db, item)
|
|
87
|
+
const description = item.description?.trim() ?? ''
|
|
88
|
+
const source = [description, typeSource].filter(Boolean).join('\n\n')
|
|
89
|
+
return { item, source, exemplars: await loadExemplars(db, item) }
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Build the prompt context from an already-authorized publication snapshot. */
|
|
93
|
+
export async function loadDekGenerationContextFromSnapshot(
|
|
94
|
+
db: D1Database,
|
|
95
|
+
snapshot: Record<string, unknown>,
|
|
96
|
+
): Promise<DekGenerationContext | null> {
|
|
97
|
+
const item = snapshotRecord(snapshot.item) as unknown as ContentItemRecord
|
|
98
|
+
if (!item.id || !item.type || !item.title) return null
|
|
99
|
+
const content = snapshotRecord(snapshot.content)
|
|
100
|
+
const typeSource =
|
|
101
|
+
item.type === 'video'
|
|
102
|
+
? String(content.script ?? '').trim()
|
|
103
|
+
: item.type === 'podcast'
|
|
104
|
+
? String(content.transcript ?? '').trim()
|
|
105
|
+
: String(content.body_markdown || content.body_html || content.subtitle || '').trim()
|
|
106
|
+
const description = item.description?.trim() ?? ''
|
|
107
|
+
return {
|
|
108
|
+
item,
|
|
109
|
+
source: [description, typeSource].filter(Boolean).join('\n\n'),
|
|
110
|
+
exemplars: await loadExemplars(db, item),
|
|
111
|
+
}
|
|
81
112
|
}
|
|
82
113
|
|
|
83
114
|
function normalizeDek(text: string): string {
|
|
@@ -88,51 +119,111 @@ function normalizeDek(text: string): string {
|
|
|
88
119
|
.trim()
|
|
89
120
|
}
|
|
90
121
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
122
|
+
/** Dispatch and normalize a dek suggestion without creating any D1 write continuation. */
|
|
123
|
+
export async function generateContentDekSuggestion(input: {
|
|
124
|
+
context: DekGenerationContext
|
|
94
125
|
llm: LlmDispatch
|
|
95
|
-
}): Promise<
|
|
96
|
-
const context = await loadDekGenerationContext(input.db, input.contentId)
|
|
97
|
-
if (!context) {
|
|
98
|
-
return { status: 'not_found', dek: null, written: false, reason: 'not_found' }
|
|
99
|
-
}
|
|
100
|
-
|
|
126
|
+
}): Promise<GenerateContentDekSuggestionResult> {
|
|
101
127
|
const result = await input.llm.dispatch({
|
|
102
128
|
...dekPrompt({
|
|
103
|
-
title: context.item.title,
|
|
104
|
-
source: context.source,
|
|
105
|
-
exemplars: context.exemplars,
|
|
129
|
+
title: input.context.item.title,
|
|
130
|
+
source: input.context.source,
|
|
131
|
+
exemplars: input.context.exemplars,
|
|
106
132
|
}),
|
|
107
133
|
stream: false,
|
|
108
134
|
})
|
|
109
135
|
if (result instanceof ReadableStream) {
|
|
110
|
-
return { status: 'unavailable', dek: null,
|
|
136
|
+
return { status: 'unavailable', dek: null, reason: 'stream_result' }
|
|
111
137
|
}
|
|
112
138
|
if (result.status === 'unavailable') {
|
|
113
139
|
return {
|
|
114
140
|
status: 'unavailable',
|
|
115
141
|
dek: null,
|
|
116
|
-
written: false,
|
|
117
142
|
reason: result.reason ?? 'unavailable',
|
|
118
143
|
}
|
|
119
144
|
}
|
|
120
145
|
|
|
121
146
|
const dek = normalizeDek(result.text)
|
|
122
|
-
if (!dek) {
|
|
123
|
-
|
|
124
|
-
|
|
147
|
+
if (!dek) return { status: 'unavailable', dek: null, reason: 'empty_result' }
|
|
148
|
+
return { status: 'generated', dek }
|
|
149
|
+
}
|
|
125
150
|
|
|
151
|
+
/** Apply a generated suggestion only while its captured editor/publication state is current. */
|
|
152
|
+
export async function applyGeneratedContentDek(input: {
|
|
153
|
+
db: D1Database
|
|
154
|
+
contentId: string
|
|
155
|
+
dek: string
|
|
156
|
+
context: DekGenerationContext
|
|
157
|
+
expectedPublicationSnapshot?: Record<string, unknown>
|
|
158
|
+
}): Promise<GenerateContentDekResult> {
|
|
126
159
|
const write = applyAiWrite({
|
|
127
|
-
locked: parseLockedFields(context.item.ai_locked_fields),
|
|
160
|
+
locked: parseLockedFields(input.context.item.ai_locked_fields),
|
|
128
161
|
field: 'excerpt',
|
|
129
|
-
value: dek,
|
|
162
|
+
value: input.dek,
|
|
130
163
|
})
|
|
131
|
-
if (!write.written) return { status: 'locked', dek, written: false }
|
|
164
|
+
if (!write.written) return { status: 'locked', dek: input.dek, written: false }
|
|
165
|
+
|
|
166
|
+
const capturedItem = input.context.item as ContentItemRecord & { canonical_version: number }
|
|
167
|
+
const guardPayload = input.expectedPublicationSnapshot
|
|
168
|
+
? JSON.stringify({
|
|
169
|
+
...input.expectedPublicationSnapshot,
|
|
170
|
+
publication_guard: CURRENT_PUBLICATION_GUARD,
|
|
171
|
+
})
|
|
172
|
+
: null
|
|
173
|
+
const publicationPredicate = guardPayload
|
|
174
|
+
? `AND EXISTS (
|
|
175
|
+
SELECT 1 FROM (SELECT ? AS payload_json) dek_expected
|
|
176
|
+
WHERE ${currentPublicationSqlGuard('dek_expected')}
|
|
177
|
+
)`
|
|
178
|
+
: ''
|
|
179
|
+
const statement = input.db
|
|
180
|
+
.prepare(
|
|
181
|
+
`UPDATE content_items
|
|
182
|
+
SET excerpt = ?, updated_at = unixepoch()
|
|
183
|
+
WHERE id = ?
|
|
184
|
+
AND canonical_version IS ?
|
|
185
|
+
AND excerpt IS ?
|
|
186
|
+
AND ai_locked_fields IS ?
|
|
187
|
+
${publicationPredicate}`,
|
|
188
|
+
)
|
|
189
|
+
.bind(
|
|
190
|
+
input.dek,
|
|
191
|
+
input.contentId,
|
|
192
|
+
capturedItem.canonical_version,
|
|
193
|
+
capturedItem.excerpt,
|
|
194
|
+
capturedItem.ai_locked_fields,
|
|
195
|
+
...(guardPayload ? [guardPayload] : []),
|
|
196
|
+
)
|
|
197
|
+
const result = await statement.run()
|
|
198
|
+
if (result.meta?.changes !== 1) {
|
|
199
|
+
return { status: 'unavailable', dek: null, written: false, reason: 'content_changed' }
|
|
200
|
+
}
|
|
201
|
+
return { status: 'generated', dek: input.dek, written: true }
|
|
202
|
+
}
|
|
132
203
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
204
|
+
export async function generateContentDek(input: {
|
|
205
|
+
db: D1Database
|
|
206
|
+
contentId: string
|
|
207
|
+
llm: LlmDispatch
|
|
208
|
+
}): Promise<GenerateContentDekResult> {
|
|
209
|
+
const context = await loadDekGenerationContext(input.db, input.contentId)
|
|
210
|
+
if (!context) {
|
|
211
|
+
return { status: 'not_found', dek: null, written: false, reason: 'not_found' }
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const suggestion = await generateContentDekSuggestion({ context, llm: input.llm })
|
|
215
|
+
if (suggestion.status === 'unavailable') {
|
|
216
|
+
return {
|
|
217
|
+
status: 'unavailable',
|
|
218
|
+
dek: null,
|
|
219
|
+
written: false,
|
|
220
|
+
reason: suggestion.reason,
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return applyGeneratedContentDek({
|
|
224
|
+
db: input.db,
|
|
225
|
+
contentId: input.contentId,
|
|
226
|
+
dek: suggestion.dek,
|
|
227
|
+
context,
|
|
228
|
+
})
|
|
138
229
|
}
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
// on `processing_trigger_token` and deduplicated on `event_id`.
|
|
11
11
|
|
|
12
12
|
import type { FoundryDispatch } from '../providers/types.js'
|
|
13
|
-
import type { D1Database } from './d1.js'
|
|
13
|
+
import type { D1Database, D1Result } from './d1.js'
|
|
14
14
|
import { validPodcastSourceUrl } from './podcast-source.js'
|
|
15
15
|
|
|
16
16
|
interface PodcastDispatchSnapshot {
|
|
@@ -35,8 +35,10 @@ export type ProcessingState =
|
|
|
35
35
|
const TERMINAL_STATES: ProcessingState[] = ['ready', 'failed']
|
|
36
36
|
|
|
37
37
|
export interface DispatchResult {
|
|
38
|
-
|
|
38
|
+
/** Podcast capture reuse has no persisted correlation to return. */
|
|
39
|
+
correlationId: string | null
|
|
39
40
|
triggerToken: string
|
|
41
|
+
skipped?: boolean
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
export class FoundryDispatchPrerequisiteError extends Error {
|
|
@@ -99,9 +101,23 @@ function inferVideoSourceKind(sourceUrl: string): string {
|
|
|
99
101
|
return 'http_url'
|
|
100
102
|
}
|
|
101
103
|
|
|
104
|
+
/**
|
|
105
|
+
* Mint a fresh source identity for the caller's source-snapshot CAS. Capture
|
|
106
|
+
* time records initiation, not worker admission; this helper does not dispatch
|
|
107
|
+
* work or change readiness. The raw token stays in the private capture record.
|
|
108
|
+
*/
|
|
109
|
+
export function createFoundrySourceCaptureToken(
|
|
110
|
+
capturedAtSeconds = Math.floor(Date.now() / 1000),
|
|
111
|
+
): string {
|
|
112
|
+
if (!Number.isSafeInteger(capturedAtSeconds) || capturedAtSeconds <= 0) {
|
|
113
|
+
throw new RangeError('Capture time must be positive safe integer Unix seconds')
|
|
114
|
+
}
|
|
115
|
+
return `fronts-source-v2:${capturedAtSeconds}:${crypto.randomUUID()}`
|
|
116
|
+
}
|
|
117
|
+
|
|
102
118
|
/**
|
|
103
119
|
* Dispatch a video or podcast content item to Foundry via the injected hook.
|
|
104
|
-
* Mints a fresh `processing_trigger_token` (UUID), calls
|
|
120
|
+
* Mints a fresh versioned `processing_trigger_token` (capture time + UUID), calls
|
|
105
121
|
* `hook.dispatchVideo({ contentId, sourceUrl, kind, durationSeconds })`, and writes
|
|
106
122
|
* `processing_correlation_id` + advances to `'queued'` in either
|
|
107
123
|
* `video_content` or `podcast_content`.
|
|
@@ -112,6 +128,7 @@ export async function dispatchToFoundry(
|
|
|
112
128
|
db: D1Database,
|
|
113
129
|
contentId: string,
|
|
114
130
|
hook: Pick<FoundryDispatch, 'dispatchVideo'>,
|
|
131
|
+
options: { ifUncaptured?: boolean } = {},
|
|
115
132
|
): Promise<DispatchResult> {
|
|
116
133
|
const content = await db
|
|
117
134
|
.prepare(
|
|
@@ -135,7 +152,7 @@ export async function dispatchToFoundry(
|
|
|
135
152
|
// Determine which media table owns this content id.
|
|
136
153
|
const videoRow = await db
|
|
137
154
|
.prepare(
|
|
138
|
-
'SELECT content_id, video_id, duration_seconds, processing_source_url, processing_source_kind FROM video_content WHERE content_id = ?',
|
|
155
|
+
'SELECT content_id, video_id, duration_seconds, processing_source_url, processing_source_kind, processing_trigger_token, processing_correlation_id FROM video_content WHERE content_id = ?',
|
|
139
156
|
)
|
|
140
157
|
.bind(contentId)
|
|
141
158
|
.first<{
|
|
@@ -144,6 +161,8 @@ export async function dispatchToFoundry(
|
|
|
144
161
|
duration_seconds: number | null
|
|
145
162
|
processing_source_url: string | null
|
|
146
163
|
processing_source_kind: string | null
|
|
164
|
+
processing_trigger_token: string | null
|
|
165
|
+
processing_correlation_id: string | null
|
|
147
166
|
}>()
|
|
148
167
|
|
|
149
168
|
let table: 'video_content' | 'podcast_content'
|
|
@@ -155,6 +174,20 @@ export async function dispatchToFoundry(
|
|
|
155
174
|
let podcastSnapshot: PodcastDispatchSnapshot | null = null
|
|
156
175
|
|
|
157
176
|
if (videoRow) {
|
|
177
|
+
if (content.type !== 'video' || content.deleted_at != null || content.status === 'archived') {
|
|
178
|
+
throw new Error(`dispatchToFoundry: video ${contentId} is inactive`)
|
|
179
|
+
}
|
|
180
|
+
if (
|
|
181
|
+
options.ifUncaptured &&
|
|
182
|
+
videoRow.processing_trigger_token &&
|
|
183
|
+
videoRow.processing_correlation_id
|
|
184
|
+
) {
|
|
185
|
+
return {
|
|
186
|
+
correlationId: videoRow.processing_correlation_id,
|
|
187
|
+
triggerToken: videoRow.processing_trigger_token,
|
|
188
|
+
skipped: true,
|
|
189
|
+
}
|
|
190
|
+
}
|
|
158
191
|
table = 'video_content'
|
|
159
192
|
sourceUrl = videoRow.processing_source_url?.trim() ?? ''
|
|
160
193
|
if (!sourceUrl) {
|
|
@@ -199,8 +232,22 @@ export async function dispatchToFoundry(
|
|
|
199
232
|
) {
|
|
200
233
|
throw new FoundryDispatchPrerequisiteError(contentId)
|
|
201
234
|
}
|
|
235
|
+
if (
|
|
236
|
+
options.ifUncaptured &&
|
|
237
|
+
podcastSnapshot?.processing_trigger_token?.trim() &&
|
|
238
|
+
podcastSnapshot.processing_source_kind === 'https' &&
|
|
239
|
+
validPodcastSourceUrl(podcastSnapshot.processing_source_url)
|
|
240
|
+
) {
|
|
241
|
+
// Source edits clear the token. This source/token pair is the CMS's
|
|
242
|
+
// committed capture evidence; a metadata save need not call the host again.
|
|
243
|
+
return {
|
|
244
|
+
correlationId: null,
|
|
245
|
+
triggerToken: podcastSnapshot.processing_trigger_token,
|
|
246
|
+
skipped: true,
|
|
247
|
+
}
|
|
248
|
+
}
|
|
202
249
|
|
|
203
|
-
const triggerToken =
|
|
250
|
+
const triggerToken = createFoundrySourceCaptureToken()
|
|
204
251
|
const processingTriggerTokenHash = `sha256:${await sha256Hex(triggerToken)}`
|
|
205
252
|
const { correlationId, podcastSource } = await hook.dispatchVideo({
|
|
206
253
|
contentId,
|
|
@@ -217,7 +264,8 @@ export async function dispatchToFoundry(
|
|
|
217
264
|
const now = Math.floor(Date.now() / 1000)
|
|
218
265
|
|
|
219
266
|
if (table === 'video_content') {
|
|
220
|
-
|
|
267
|
+
if (!videoRow) throw new Error('Video dispatch snapshot is missing')
|
|
268
|
+
const written = await db
|
|
221
269
|
.prepare(
|
|
222
270
|
`UPDATE video_content
|
|
223
271
|
SET processing_state = 'queued',
|
|
@@ -235,10 +283,33 @@ export async function dispatchToFoundry(
|
|
|
235
283
|
transcript_url = NULL,
|
|
236
284
|
hls_manifest_url = NULL,
|
|
237
285
|
hls_poster_url = NULL
|
|
238
|
-
WHERE content_id =
|
|
286
|
+
WHERE content_id = ? AND video_id IS ? AND duration_seconds IS ?
|
|
287
|
+
AND processing_source_url IS ? AND processing_source_kind IS ?
|
|
288
|
+
AND processing_trigger_token IS ? AND processing_correlation_id IS ?
|
|
289
|
+
AND EXISTS (SELECT 1 FROM content_items ci WHERE ci.id = video_content.content_id
|
|
290
|
+
AND ci.type = 'video' AND ci.slug = ? AND ci.canonical_version = ?
|
|
291
|
+
AND ci.status = ? AND ci.deleted_at IS NULL)`,
|
|
292
|
+
)
|
|
293
|
+
.bind(
|
|
294
|
+
videoId,
|
|
295
|
+
sourceKind,
|
|
296
|
+
correlationId,
|
|
297
|
+
triggerToken,
|
|
298
|
+
now,
|
|
299
|
+
contentId,
|
|
300
|
+
videoRow.video_id,
|
|
301
|
+
videoRow.duration_seconds,
|
|
302
|
+
videoRow.processing_source_url,
|
|
303
|
+
videoRow.processing_source_kind,
|
|
304
|
+
videoRow.processing_trigger_token,
|
|
305
|
+
videoRow.processing_correlation_id,
|
|
306
|
+
content.slug,
|
|
307
|
+
content.canonical_version,
|
|
308
|
+
content.status,
|
|
239
309
|
)
|
|
240
|
-
.bind(videoId, sourceKind, correlationId, triggerToken, now, contentId)
|
|
241
310
|
.run()
|
|
311
|
+
if (written.meta?.changes !== 1)
|
|
312
|
+
throw new Error('Video source changed before dispatch acknowledgement')
|
|
242
313
|
} else {
|
|
243
314
|
if (!podcastSnapshot) throw new Error('Podcast dispatch snapshot is missing')
|
|
244
315
|
if (
|
|
@@ -259,7 +330,7 @@ export async function dispatchToFoundry(
|
|
|
259
330
|
// source/audio/editor snapshot is still current. No stale acknowledgement
|
|
260
331
|
// can restore a replaced source or overwrite a newer processing token.
|
|
261
332
|
const written = await db
|
|
262
|
-
.prepare(`UPDATE podcast_content SET processing_source_url = ?, processing_source_kind = ?, processing_trigger_token =
|
|
333
|
+
.prepare(`UPDATE podcast_content SET processing_source_url = ?, processing_source_kind = ?, processing_trigger_token = ?, transcript = ''
|
|
263
334
|
WHERE content_id = ? AND audio_r2_key IS ? AND duration_seconds IS ? AND transcript IS ?
|
|
264
335
|
AND processing_source_url IS ? AND processing_source_kind IS ? AND processing_trigger_token IS ?
|
|
265
336
|
AND EXISTS (SELECT 1 FROM content_items ci WHERE ci.id = podcast_content.content_id
|
|
@@ -372,12 +443,12 @@ export async function applyFoundryCallback(db: D1Database, input: CallbackInput)
|
|
|
372
443
|
? Math.max(0, Math.floor(durationSeconds))
|
|
373
444
|
: null
|
|
374
445
|
if (nextTranscript !== null || nextDurationSeconds !== null) {
|
|
375
|
-
await db
|
|
446
|
+
const updated = await db
|
|
376
447
|
.prepare(
|
|
377
448
|
`UPDATE podcast_content
|
|
378
449
|
SET transcript = CASE WHEN ? THEN ? ELSE transcript END,
|
|
379
450
|
duration_seconds = CASE WHEN ? THEN ? ELSE duration_seconds END
|
|
380
|
-
WHERE content_id = ?`,
|
|
451
|
+
WHERE content_id = ? AND processing_trigger_token = ?`,
|
|
381
452
|
)
|
|
382
453
|
.bind(
|
|
383
454
|
nextTranscript !== null ? 1 : 0,
|
|
@@ -385,21 +456,26 @@ export async function applyFoundryCallback(db: D1Database, input: CallbackInput)
|
|
|
385
456
|
nextDurationSeconds !== null ? 1 : 0,
|
|
386
457
|
nextDurationSeconds,
|
|
387
458
|
podcastRow.content_id,
|
|
459
|
+
triggerToken,
|
|
388
460
|
)
|
|
389
461
|
.run()
|
|
462
|
+
if (updated.meta?.changes !== 1) return false
|
|
390
463
|
}
|
|
391
464
|
|
|
392
465
|
const nextDescription =
|
|
393
466
|
typeof description === 'string' && description.trim() ? description.trim() : null
|
|
394
467
|
const nextExcerpt = typeof excerpt === 'string' && excerpt.trim() ? excerpt.trim() : null
|
|
395
468
|
if (nextDescription !== null || nextExcerpt !== null) {
|
|
396
|
-
await db
|
|
469
|
+
const updated = await db
|
|
397
470
|
.prepare(
|
|
398
471
|
`UPDATE content_items
|
|
399
472
|
SET description = CASE WHEN ? THEN ? ELSE description END,
|
|
400
473
|
excerpt = CASE WHEN ? THEN ? ELSE excerpt END,
|
|
401
474
|
updated_at = unixepoch()
|
|
402
|
-
WHERE id =
|
|
475
|
+
WHERE id = ? AND EXISTS (
|
|
476
|
+
SELECT 1 FROM podcast_content p WHERE p.content_id = content_items.id
|
|
477
|
+
AND p.processing_trigger_token = ?
|
|
478
|
+
)`,
|
|
403
479
|
)
|
|
404
480
|
.bind(
|
|
405
481
|
nextDescription !== null ? 1 : 0,
|
|
@@ -407,50 +483,60 @@ export async function applyFoundryCallback(db: D1Database, input: CallbackInput)
|
|
|
407
483
|
nextExcerpt !== null ? 1 : 0,
|
|
408
484
|
nextExcerpt,
|
|
409
485
|
podcastRow.content_id,
|
|
486
|
+
triggerToken,
|
|
410
487
|
)
|
|
411
488
|
.run()
|
|
489
|
+
if (updated.meta?.changes !== 1) return false
|
|
412
490
|
}
|
|
413
491
|
|
|
414
|
-
return
|
|
492
|
+
return Boolean(
|
|
493
|
+
await db
|
|
494
|
+
.prepare(
|
|
495
|
+
'SELECT content_id FROM podcast_content WHERE content_id = ? AND processing_trigger_token = ?',
|
|
496
|
+
)
|
|
497
|
+
.bind(podcastRow.content_id, triggerToken)
|
|
498
|
+
.first(),
|
|
499
|
+
)
|
|
415
500
|
}
|
|
416
501
|
|
|
417
502
|
if (!videoRow) return false
|
|
418
503
|
const row = videoRow
|
|
504
|
+
let updated: D1Result
|
|
419
505
|
|
|
420
506
|
if (nextState === 'failed') {
|
|
421
|
-
await db
|
|
507
|
+
updated = await db
|
|
422
508
|
.prepare(
|
|
423
509
|
`UPDATE video_content
|
|
424
510
|
SET processing_state = ?,
|
|
425
511
|
processing_last_event_at = ?,
|
|
426
512
|
processing_completed_at = ?,
|
|
427
513
|
processing_error = ?
|
|
428
|
-
WHERE content_id = ?`,
|
|
514
|
+
WHERE content_id = ? AND processing_trigger_token = ?`,
|
|
429
515
|
)
|
|
430
|
-
.bind(nextState, now, now, error ?? null, row.content_id)
|
|
516
|
+
.bind(nextState, now, now, error ?? null, row.content_id, triggerToken)
|
|
431
517
|
.run()
|
|
432
518
|
} else if (isTerminal) {
|
|
433
519
|
// 'ready'
|
|
434
|
-
await db
|
|
520
|
+
updated = await db
|
|
435
521
|
.prepare(
|
|
436
522
|
`UPDATE video_content
|
|
437
523
|
SET processing_state = ?,
|
|
438
524
|
processing_last_event_at = ?,
|
|
439
525
|
processing_completed_at = ?
|
|
440
|
-
WHERE content_id = ?`,
|
|
526
|
+
WHERE content_id = ? AND processing_trigger_token = ?`,
|
|
441
527
|
)
|
|
442
|
-
.bind(nextState, now, now, row.content_id)
|
|
528
|
+
.bind(nextState, now, now, row.content_id, triggerToken)
|
|
443
529
|
.run()
|
|
444
530
|
} else {
|
|
445
|
-
await db
|
|
531
|
+
updated = await db
|
|
446
532
|
.prepare(
|
|
447
|
-
'UPDATE video_content SET processing_state = ?, processing_last_event_at = ? WHERE content_id = ?',
|
|
533
|
+
'UPDATE video_content SET processing_state = ?, processing_last_event_at = ? WHERE content_id = ? AND processing_trigger_token = ?',
|
|
448
534
|
)
|
|
449
|
-
.bind(nextState, now, row.content_id)
|
|
535
|
+
.bind(nextState, now, row.content_id, triggerToken)
|
|
450
536
|
.run()
|
|
451
537
|
}
|
|
452
538
|
|
|
453
|
-
return
|
|
539
|
+
return updated.meta?.changes === 1
|
|
454
540
|
}
|
|
455
541
|
|
|
456
542
|
/**
|