Skip to content

Commit e229342

Browse files
committed
feat(write): pre-generate selected AI resources before publish
Keep posts and notes as drafts until chosen summary, insights, translation, and TTS tasks finish, then publish with skip flags so auto-generation does not run twice.
1 parent 41fc406 commit e229342

22 files changed

Lines changed: 780 additions & 51 deletions

apps/admin/src/api/notes.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export interface CreateNoteData {
3939
mood?: string
4040
password?: null | string
4141
publicAt?: Date | null | string
42+
skipAiAutoGeneration?: boolean
4243
slug?: string
4344
text: string
4445
title: string
@@ -88,13 +89,18 @@ export function patchNote(id: string, data: PatchNoteData) {
8889
return patchJson<NoteModel, PatchNoteData>(`/notes/${id}`, data)
8990
}
9091

91-
export function patchNotePublish(id: string, isPublished: boolean) {
92-
return patchJson<NoteModel, { isPublished: boolean }>(
93-
`/notes/${id}/publish`,
94-
{
95-
isPublished,
96-
},
97-
)
92+
export function patchNotePublish(
93+
id: string,
94+
isPublished: boolean,
95+
preparedAiResources?: string[],
96+
) {
97+
return patchJson<
98+
NoteModel,
99+
{ isPublished: boolean; preparedAiResources?: string[] }
100+
>(`/notes/${id}/publish`, {
101+
isPublished,
102+
preparedAiResources,
103+
})
98104
}
99105

100106
export function deleteNote(id: string) {

apps/admin/src/api/posts.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ export interface CreatePostData {
3434
meta?: Record<string, unknown>
3535
pin?: null | string
3636
pinOrder?: null | number
37+
preparedAiResources?: string[]
3738
relatedId?: string[]
39+
skipAiAutoGeneration?: boolean
3840
slug?: string
3941
summary?: null | string
4042
tags?: string[]
@@ -72,8 +74,20 @@ export function updatePost(id: string, data: Partial<CreatePostData>) {
7274
return putJson<PostModel, Partial<CreatePostData>>(`/posts/${id}`, data)
7375
}
7476

75-
export function patchPost(id: string, data: Partial<PostModel>) {
76-
return patchJson<PostModel, Partial<PostModel>>(`/posts/${id}`, data)
77+
export function patchPost(
78+
id: string,
79+
data: Partial<PostModel> & {
80+
preparedAiResources?: string[]
81+
skipAiAutoGeneration?: boolean
82+
},
83+
) {
84+
return patchJson<
85+
PostModel,
86+
Partial<PostModel> & {
87+
preparedAiResources?: string[]
88+
skipAiAutoGeneration?: boolean
89+
}
90+
>(`/posts/${id}`, data)
7791
}
7892

7993
export function deletePost(id: string) {

apps/admin/src/data/resources/note.mutations.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ async function ensureNoteHydrated(id: string): Promise<void> {
2020
export async function publishNote(
2121
id: string,
2222
isPublished: boolean,
23+
options?: { preparedAiResources?: string[] },
2324
): Promise<NoteModel> {
2425
await ensureNoteHydrated(id)
2526
const tx = createTransaction()
2627
tx.update(notes, id, (draft) => {
2728
draft.isPublished = isPublished
2829
})
29-
const result = await tx.commit(() => patchNotePublish(id, isPublished))
30+
const result = await tx.commit(async () => {
31+
await patchNotePublish(id, isPublished, options?.preparedAiResources)
32+
return getNoteById(id, { single: true })
33+
})
3034
notes.hydrate([result])
3135
return result
3236
}

apps/admin/src/data/resources/post.mutations.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import type { CreatePostData } from '~/api/posts'
2-
import { createPost, deletePost, getPostById, updatePost } from '~/api/posts'
2+
import {
3+
createPost,
4+
deletePost,
5+
getPostById,
6+
patchPost,
7+
updatePost,
8+
} from '~/api/posts'
39
import { createTransaction } from '~/data/resource/transaction'
410
import type { PostModel } from '~/models/post'
511

@@ -14,8 +20,26 @@ async function ensurePostHydrated(id: string): Promise<void> {
1420
export async function publishPost(
1521
id: string,
1622
isPublished: boolean,
23+
options?: {
24+
preparedAiResources?: string[]
25+
skipAiAutoGeneration?: boolean
26+
},
1727
): Promise<PostModel | void> {
1828
await ensurePostHydrated(id)
29+
if (options?.skipAiAutoGeneration || options?.preparedAiResources?.length) {
30+
const tx = createTransaction()
31+
tx.update(posts, id, (draft) => {
32+
draft.isPublished = isPublished
33+
})
34+
return tx.commit(async () => {
35+
await patchPost(id, {
36+
isPublished,
37+
preparedAiResources: options.preparedAiResources,
38+
skipAiAutoGeneration: options.skipAiAutoGeneration,
39+
})
40+
return posts.get(id)
41+
})
42+
}
1943
return posts.update(id, (draft) => {
2044
draft.isPublished = isPublished
2145
})

0 commit comments

Comments
 (0)