thread-master/apps/web/src/data/repos.ts

619 lines
20 KiB
TypeScript
Raw Normal View History

2026-07-10 05:10:31 +00:00
import type {
AiSettings,
AppNotification,
Brand,
BrandProduct,
DataSource,
ExternalThreadTarget,
InspirationIdea,
InspireAngle,
InspireChatMessage,
InspireElement,
InspireSession,
2026-07-13 01:15:30 +00:00
InspireSessionSummary,
2026-07-10 12:54:45 +00:00
InviteMemberBrief,
InviteTreeNode,
2026-07-10 05:10:31 +00:00
Job,
Member,
MentionItem,
2026-07-10 12:54:45 +00:00
MyInviteNetwork,
2026-07-10 05:10:31 +00:00
OwnPost,
OutboxBundle,
Persona,
PlacementSettings,
ResearchHit,
Role,
ScoutHomeworkRecord,
ScoutPost,
ScoutRunBrief,
ScoutScanContext,
ScoutTopic,
ThreadPlay,
ThreadsAccount,
TokenPair,
TrendItem,
TrendKind,
ViralAnalysis,
ViralSample,
} from "../domain/types";
import type { AdminUserListPage, MemberAdminView } from "../lib/tenantUsers";
import type { PlanPurchase } from "../lib/planPurchase";
import type {
PlanId,
TenantAnalyticsQuery,
TenantUsageAnalytics,
TenantUsageSummary,
UsageEvent,
UsageMemberPrefs,
UsageMonthSummary,
} from "../lib/usageMeter";
export type MemberProfilePatch = {
display_name?: string;
2026-07-10 12:54:45 +00:00
nickname?: string;
full_name?: string;
2026-07-10 05:10:31 +00:00
email?: string;
2026-07-10 12:54:45 +00:00
phone?: string;
2026-07-10 05:10:31 +00:00
bio?: string;
timezone?: string;
notify_email?: boolean;
/**
* URLdata URL https
* `null` `undefined`
*/
avatar_url?: string | null;
2026-07-10 12:54:45 +00:00
gender_code?: number;
birthdate?: number;
national?: string;
address?: string;
post_code?: string;
preferred_language?: string;
currency?: string;
2026-07-10 05:10:31 +00:00
/** 改密時必填 */
current_password?: string;
new_password?: string;
};
export type AuthRepo = {
login(email: string, password: string): Promise<{ tokens: TokenPair; member: Member }>;
logout(): Promise<void>;
me(): Promise<Member | null>;
/** 更新自己的會員資料mock 寫 session */
updateProfile(patch: MemberProfilePatch): Promise<Member>;
/**
* mock
* mock_reset_path 便
*/
requestPasswordReset(email: string): Promise<{
ok: true;
message: string;
}>;
2026-07-10 12:54:45 +00:00
/**
* token
* email livemock token
*/
resetPassword(
token: string,
newPassword: string,
email?: string,
): Promise<{ ok: true; message: string }>;
2026-07-10 05:10:31 +00:00
/** 寄送信箱驗證碼(需已登入) */
sendEmailVerificationCode(): Promise<{
ok: true;
message: string;
}>;
/** 輸入驗證碼完成信箱驗證 */
verifyEmail(code: string): Promise<Member>;
2026-07-10 12:54:45 +00:00
/**
* MinIO/S3 URL
* content data URLkind avatar
*/
2026-07-13 01:15:30 +00:00
/** kind: avatar 永久temp 發文暫存(發成功/取消 outbox 後刪other 相容 */
uploadImage(content: string, kind?: "avatar" | "other" | "temp"): Promise<{ url: string; key?: string }>;
2026-07-10 05:10:31 +00:00
};
/** 管理者新增島民 */
export type AdminCreateMemberInput = {
display_name: string;
email: string;
password?: string;
roles?: Role[];
email_verified?: boolean;
bio?: string;
2026-07-10 12:54:45 +00:00
/** 邀請人 uid未傳 = 掛建立者null = 根 */
parent_uid?: string | null;
};
/**
* mock
* + +
* + 調
*/
export type InviteRepo = {
/** 目前登入者的邀請網絡 */
getMyNetwork(): Promise<MyInviteNetwork>;
/**
*
*
*/
applyInviteCode(code: string): Promise<MyInviteNetwork>;
/** 管理員:邀請森林 */
getTree(): Promise<InviteTreeNode[]>;
/** 管理員改邀請人null = 獨立根) */
moveMember(uid: string, newParentUid: string | null): Promise<InviteMemberBrief>;
/** 管理員:可當邀請人的候選(排除 subtree */
listParentCandidates(excludeSubtreeRootUid?: string): Promise<InviteMemberBrief[]>;
2026-07-10 05:10:31 +00:00
};
/** 管理員:島民列表與可寫操作 */
export type AdminUsersRepo = {
/** @deprecated 請用 listUsersPage */
listUsers(): Promise<MemberAdminView[]>;
/** 分頁列表 page 從 1 起query 可搜名稱 / Email / uid */
listUsersPage(opts?: {
page?: number;
pageSize?: number;
query?: string;
}): Promise<AdminUserListPage>;
getUser(uid: string): Promise<MemberAdminView | null>;
/** 新增島民(回傳臨時密碼) */
createMember(
input: AdminCreateMemberInput,
): Promise<{ user: MemberAdminView; temporary_password: string }>;
/** 停權 / 復權 */
setSuspended(uid: string, suspended: boolean): Promise<MemberAdminView>;
/** 改驗證狀態 */
setEmailVerified(uid: string, verified: boolean): Promise<MemberAdminView>;
/** 指派角色member / admin */
setRoles(uid: string, roles: Role[]): Promise<MemberAdminView>;
/**
* 使 newPassword
* temporary_password
*/
resetPassword(
uid: string,
newPassword?: string,
): Promise<{ user: MemberAdminView; temporary_password: string }>;
};
export type AccountsRepo = {
list(): Promise<ThreadsAccount[]>;
2026-07-13 01:15:30 +00:00
/** mock 專用:假連帳 */
2026-07-10 05:10:31 +00:00
createMock(): Promise<ThreadsAccount>;
2026-07-13 01:15:30 +00:00
/** liveOAuth start回授權 URLfake provider 會指回 callback */
oauthStart(): Promise<{ authorize_url: string; state: string }>;
2026-07-10 05:10:31 +00:00
remove(id: string): Promise<void>;
refreshSession(id: string): Promise<ThreadsAccount>;
};
export type PlaysRepo = {
list(): Promise<ThreadPlay[]>;
listByPost(ownPostId: string): Promise<ThreadPlay[]>;
/** 依外部 Threads 連結列出方案URL 正規化後比對) */
listByExternalUrl(url: string): Promise<ThreadPlay[]>;
/** 貼連結 → mock 解析目標貼文 */
resolveExternalLink(url: string): Promise<ExternalThreadTarget>;
get(id: string): Promise<ThreadPlay | null>;
save(play: ThreadPlay): Promise<ThreadPlay>;
remove(id: string): Promise<void>;
submit(id: string): Promise<OutboxBundle>;
2026-07-13 01:15:30 +00:00
/** 真 AI 產劇本一步reply / root */
generateStep(opts: {
personaId?: string;
context: string;
topic?: string;
speakerLabel?: string;
isLead?: boolean;
mode?: "reply" | "root";
}): Promise<string>;
/**
* Job
* play DB reload play
*/
generateScript(playId: string): Promise<{ jobId: string; async: boolean }>;
2026-07-10 05:10:31 +00:00
};
export type OutboxRepo = {
list(): Promise<OutboxBundle[]>;
get(id: string): Promise<OutboxBundle | null>;
simulateSuccess(id: string): Promise<OutboxBundle>;
simulateRootFail(id: string): Promise<OutboxBundle>;
retryStep(bundleId: string, stepId: string): Promise<OutboxBundle>;
/** 刪除整筆發送佇列項目 */
remove(id: string): Promise<void>;
};
2026-07-13 03:18:08 +00:00
/** 任務列表分桶:定期排程 / 執行中 / 歷史 */
export type JobListTab = "recurring" | "active" | "history";
export type JobListQuery = {
tab?: JobListTab;
page?: number;
pageSize?: number;
};
export type JobListResult = {
list: Job[];
pagination: {
page: number;
pageSize: number;
total: number;
totalPages: number;
};
};
2026-07-10 05:10:31 +00:00
export type JobsRepo = {
2026-07-13 03:18:08 +00:00
/**
* query tab+page query active 50
* JobListResult .list
*/
list(query?: JobListQuery): Promise<JobListResult>;
2026-07-10 05:10:31 +00:00
get(id: string): Promise<Job | null>;
startDemo(): Promise<Job>;
2026-07-13 01:15:30 +00:00
/** 手動刪除(執行中不可刪;後端終態約 2 天後也會自動清) */
remove(id: string): Promise<void>;
2026-07-10 05:10:31 +00:00
};
export type NotificationsRepo = {
list(): Promise<AppNotification[]>;
unreadCount(): Promise<number>;
markRead(id: string): Promise<void>;
markAllRead(): Promise<void>;
};
2026-07-13 01:15:30 +00:00
/** Threads OAuth 平台狀態App Secret 不回傳) */
export type ThreadsPlatformStatus = {
provider: "fake" | "meta" | string;
configured: boolean;
oauth_ready: boolean;
connect_path: string;
/** Meta Valid OAuth Redirect URIs 用 */
callback_url: string;
public_web_base: string;
app_id_masked?: string;
hint: string;
};
2026-07-10 05:10:31 +00:00
export type SettingsRepo = {
getDataSource(): DataSource;
setDataSource(ds: DataSource): void;
2026-07-13 01:15:30 +00:00
/**
* selected model+
* provider provider models
*/
getAi(provider?: string): Promise<AiSettings>;
2026-07-10 05:10:31 +00:00
saveAi(patch: Partial<AiSettings> & { api_key?: string; research_api_key?: string }): Promise<AiSettings>;
getPlacement(): Promise<PlacementSettings>;
savePlacement(
patch: Partial<PlacementSettings> & { brave_api_key?: string; exa_api_key?: string },
): Promise<PlacementSettings>;
2026-07-13 01:15:30 +00:00
/** @deprecated 優先用 getAi(provider) 一併取 models + selected_model */
2026-07-10 05:10:31 +00:00
listModels(provider: string): Promise<string[]>;
2026-07-13 01:15:30 +00:00
/** 平台 Threads App 是否已接 Meta設定頁狀態卡 */
getThreadsStatus(): Promise<ThreadsPlatformStatus>;
2026-07-10 05:10:31 +00:00
};
export type PersonasRepo = {
list(): Promise<Persona[]>;
get(id: string): Promise<Persona | null>;
getActiveId(): Promise<string>;
setActiveId(id: string): Promise<void>;
save(persona: Persona): Promise<Persona>;
remove(id: string): Promise<void>;
analyzeFromText(id: string, rawText: string, sourceLabel?: string): Promise<Persona>;
analyzeFromAccount(id: string, username: string): Promise<Persona>;
};
export type OwnPostsRepo = {
list(accountId?: string): Promise<OwnPost[]>;
lastSyncedAt(): Promise<number | null>;
sync(accountId: string): Promise<OwnPost[]>;
2026-07-13 01:15:30 +00:00
/** 點開貼文後才載留言(避免同步時一次打爆 API */
loadReplies(postId: string): Promise<OwnPost>;
2026-07-10 05:10:31 +00:00
generateReply(opts: {
postId: string;
replyId?: string;
personaId?: string;
}): Promise<string>;
sendReply(opts: {
postId: string;
replyId?: string;
text: string;
/** 用哪個帳號送出mock 寫入 username */
accountId?: string;
/** 附圖 URLmock 記張數/可選縮圖) */
imageUrls?: string[];
}): Promise<OwnPost>;
analyzePost(postId: string): Promise<OwnPost>;
generateFromFormula(postId: string, personaId?: string): Promise<{ title: string; topic: string; root: string }>;
};
export type MentionsRepo = {
list(accountId?: string): Promise<MentionItem[]>;
2026-07-13 01:15:30 +00:00
/** 從 Threads Graph 同步「@我」提及(需 threads_manage_mentions */
sync(accountId: string): Promise<MentionItem[]>;
2026-07-10 05:10:31 +00:00
generateReply(id: string, personaId?: string): Promise<MentionItem>;
markReplied(id: string, text?: string, imageUrls?: string[]): Promise<MentionItem>;
skip(id: string): Promise<MentionItem>;
};
export type InspirationRepo = {
list(): Promise<InspirationIdea[]>;
/** 預設靈感榜Threads 熱標(提示用) */
listTrends(kind?: TrendKind | "all"): Promise<TrendItem[]>;
refreshTrends(kind?: TrendKind | "all"): Promise<TrendItem[]>;
searchTrends(query: string): Promise<TrendItem[]>;
/** 元素庫 */
listElements(): Promise<InspireElement[]>;
saveElement(el: InspireElement): Promise<InspireElement>;
removeElement(id: string): Promise<void>;
2026-07-13 01:15:30 +00:00
/** 目前作用中的 session */
2026-07-10 05:10:31 +00:00
getSession(): Promise<InspireSession>;
saveSession(session: InspireSession): Promise<InspireSession>;
2026-07-13 01:15:30 +00:00
/**
* API session
* UI createSession
*/
2026-07-10 05:10:31 +00:00
clearSession(): Promise<InspireSession>;
2026-07-13 01:15:30 +00:00
/** 多對話列表 */
listSessions(): Promise<InspireSessionSummary[]>;
createSession(title?: string): Promise<InspireSession>;
activateSession(id: string): Promise<InspireSession>;
deleteSession(id: string): Promise<InspireSession>;
2026-07-10 05:10:31 +00:00
/**
* pinnedIds session
* mode=generate assistant draft.body
*/
chat(opts: {
message: string;
pinnedIds: string[];
mode: "chat" | "generate";
2026-07-13 01:15:30 +00:00
personaId?: string;
sessionId?: string;
/** generate待改寫素材 */
material?: string;
2026-07-10 05:10:31 +00:00
}): Promise<{ session: InspireSession; messages: InspireChatMessage[] }>;
2026-07-13 01:15:30 +00:00
/**
* onDelta session
*/
chatStream(
opts: {
message: string;
pinnedIds: string[];
mode: "chat" | "generate";
personaId?: string;
sessionId?: string;
material?: string;
},
onDelta: (chunk: string) => void,
): Promise<{
session: InspireSession;
/** 實際送 AI 的完整 prompt與預覽同組裝 */
prompt?: string;
fingerprint?: string;
charCount?: number;
runeCount?: number;
sections?: string[];
}>;
/**
* buildInspirePrompt prompt AI
* fingerprint done prompt_fingerprint
*/
previewPrompt(opts: {
message: string;
pinnedIds: string[];
mode: "chat" | "generate";
personaId?: string;
sessionId?: string;
material?: string;
}): Promise<{
prompt: string;
mode: string;
sections: string[];
blocks: Array<{ title: string; body: string }>;
fingerprint: string;
charCount: number;
runeCount: number;
pinned: Array<{ id: string; kind: string; title: string; body: string }>;
note?: string;
}>;
2026-07-10 05:10:31 +00:00
/** @deprecated 舊角度流;保留相容 */
sparkAngles(
trendId: string,
opts?: { personaId?: string | null; brandId?: string | null },
): Promise<InspireAngle[]>;
/** @deprecated */
sparkAnglesForTopic(
label: string,
opts?: { personaId?: string | null; brandId?: string | null; samples?: string[] },
): Promise<{ trend: TrendItem; angles: InspireAngle[] }>;
bookmarkTrend(trendId: string): Promise<InspirationIdea>;
saveIdea(idea: InspirationIdea): Promise<InspirationIdea>;
generate(topic: string, personaId?: string): Promise<InspirationIdea>;
listViral(): Promise<ViralSample[]>;
/** @deprecated */
sparkFromTrend(
trendId: string,
personaId?: string,
opts?: { save?: boolean },
): Promise<InspirationIdea>;
};
export type ResearchRepo = {
search(query: string): Promise<ResearchHit[]>;
};
export type MediaRepo = {
generateImage(prompt: string): Promise<{ id: string; url: string; prompt: string }>;
/** 本機選檔 → 可預覽附圖Phase B mock不真上傳 */
attachLocal(files: FileList | File[]): Promise<{ id: string; url: string; name?: string }[]>;
};
2026-07-13 01:15:30 +00:00
export type PersonaPreviewResult = {
topic: string;
topic_source: "news" | "manual" | "fallback" | string;
post_text: string;
reply_text: string;
notes?: string;
};
2026-07-10 05:10:31 +00:00
export type ComposeRepo = {
2026-07-13 01:15:30 +00:00
/**
* 仿 Job
* - async=true jobId job payload.result_text
* - async=false text
*/
mimic(
sourceText: string,
personaId?: string,
structureNotes?: string,
): Promise<{ text?: string; jobId?: string; async: boolean }>;
/** 依指紋真 LLM 試產主貼 + 回文(可抓新聞話題) */
personaPreview(opts: {
personaId: string;
topic?: string;
useNews?: boolean;
}): Promise<PersonaPreviewResult>;
2026-07-10 05:10:31 +00:00
analyzeViral(text: string): Promise<ViralAnalysis>;
/** 單篇送出 Outbox */
publishSingle(opts: {
accountId: string;
text: string;
title?: string;
imageUrls?: string[];
2026-07-10 12:54:45 +00:00
/**
* unix nanoseconds UTC
* now
*/
schedule_start_at?: number;
2026-07-13 01:15:30 +00:00
/** Threads 話題標籤topic_tag可不加 # */
topicTag?: string;
2026-07-10 05:10:31 +00:00
}): Promise<OutboxBundle>;
};
export type ScoutRepo = {
listBrands(): Promise<Brand[]>;
get(id: string): Promise<Brand | null>;
getActiveBrandId(): Promise<string>;
setActiveBrandId(id: string): Promise<void>;
/** 對齊舊 BrandsPage建立牌子 */
createBrand(input?: { display_name?: string; brief?: string }): Promise<Brand>;
saveBrand(brand: Brand): Promise<Brand>;
removeBrand(id: string): Promise<void>;
listProducts(brandId: string): Promise<BrandProduct[]>;
/** 全部產品(探查下拉用) */
listAllProducts(): Promise<BrandProduct[]>;
getProduct(id: string): Promise<BrandProduct | null>;
saveProduct(product: BrandProduct): Promise<BrandProduct>;
removeProduct(id: string): Promise<void>;
/**
* 稿
* Phase B mocklive
*/
importProductFromUrl(url: string): Promise<{
label: string;
product_context: string;
pain_points: string[];
match_tags: string[];
placement_url: string;
source_note: string;
}>;
listTopics(brandId?: string): Promise<ScoutTopic[]>;
saveTopic(topic: ScoutTopic): Promise<ScoutTopic>;
removeTopic(id: string): Promise<void>;
/**
* + brief
* = theme
*/
prepareBrief(opts: {
intent: string;
brandId?: string | null;
productId?: string | null;
/** value痛點置入預設activity關鍵字活躍 */
purpose?: "value" | "activity";
/**
*
* false deep
*/
deep?: boolean;
}): Promise<ScoutRunBrief>;
2026-07-13 08:59:13 +00:00
/** 依 brief含使用者勾選後的 scan_terms建立背景海巡任務。 */
runScanFromBrief(brief: ScoutRunBrief): Promise<{ job: Job }>;
2026-07-10 05:10:31 +00:00
/** @deprecated 用 prepareBrief */
getScanContext(brandId: string, productId?: string | null): Promise<ScoutScanContext>;
/** 列出命中brandId 空 = 全部(含主題模式) */
listPosts(brandId?: string | null): Promise<ScoutPost[]>;
/** @deprecated 用 runScanFromBrief */
runScan(
brandId: string,
productId?: string | null,
extraTerms?: string[],
2026-07-13 08:59:13 +00:00
): Promise<{ job: Job }>;
2026-07-10 05:10:31 +00:00
draftOutreach(postId: string, personaId?: string): Promise<ScoutPost>;
skipOutreach(postId: string): Promise<ScoutPost>;
markPublished(postId: string): Promise<ScoutPost>;
/**
* 稿 published
*/
sendOutreach(opts: {
postId: string;
text: string;
accountId?: string;
personaId?: string;
}): Promise<ScoutPost>;
/** 刪除單則命中 */
removePost(postId: string): Promise<void>;
/** 刪除同一主題分組下全部命中 */
removeTheme(themeKey: string): Promise<void>;
/** 已完成的功課列表(持久化) */
listHomework(): Promise<ScoutHomeworkRecord[]>;
/** 存一輪功課(同 theme_key 覆蓋) */
saveHomework(record: ScoutHomeworkRecord): Promise<ScoutHomeworkRecord>;
getHomework(themeKey: string): Promise<ScoutHomeworkRecord | null>;
removeHomework(themeKey: string): Promise<void>;
};
export type UsageRepo = {
/** 目前登入者(或指定 uid本月摘要 + 明細 */
getSummary(monthKey?: string, uid?: string): Promise<UsageMonthSummary>;
listEvents(limit?: number, uid?: string): Promise<UsageEvent[]>;
getPlanId(): Promise<PlanId>;
setPlanId(id: PlanId): Promise<void>;
getMemberPrefs(uid?: string): Promise<UsageMemberPrefs>;
/** 管理員:設某人方案/無限 */
setMemberPrefs(uid: string, patch: Partial<UsageMemberPrefs>): Promise<UsageMemberPrefs>;
/** 管理員:全體本月用量 */
getTenantSummary(monthKey?: string): Promise<TenantUsageSummary>;
/** 管理員:全體區間分析(日/月/年 + 購買 vs 消耗) */
getTenantAnalytics(query?: TenantAnalyticsQuery): Promise<TenantUsageAnalytics>;
/**
* mock plan
* setMemberPrefs
*/
purchasePlan(plan_id: PlanId, opts?: { mock_ref?: string }): Promise<PlanPurchase>;
/** 自己的購買紀錄 */
listMyPurchases(limit?: number): Promise<PlanPurchase[]>;
};
export type Repos = {
dataSource: DataSource;
auth: AuthRepo;
accounts: AccountsRepo;
plays: PlaysRepo;
outbox: OutboxRepo;
jobs: JobsRepo;
notifications: NotificationsRepo;
settings: SettingsRepo;
personas: PersonasRepo;
ownPosts: OwnPostsRepo;
mentions: MentionsRepo;
inspiration: InspirationRepo;
research: ResearchRepo;
media: MediaRepo;
compose: ComposeRepo;
scout: ScoutRepo;
usage: UsageRepo;
adminUsers: AdminUsersRepo;
2026-07-10 12:54:45 +00:00
invite: InviteRepo;
2026-07-10 05:10:31 +00:00
};