后端迁移会话主编排

This commit is contained in:
yml
2026-05-21 15:42:24 +08:00
parent e88b9d18fd
commit 17df03c65c
4 changed files with 57 additions and 20 deletions
@@ -27,9 +27,10 @@ export type TencentBrowserSession = Record<string, any> & {
autoCloseAt: number
autoCloseTimer: ReturnType<typeof setTimeout> | null
sessionDir: string
browserContext?: BrowserContextLike | null
page?: unknown
browserContext: any
page: any
browserContextClosed?: boolean
presentationSyncAttempts: number
}
type RegisterSessionInput = {
@@ -1,5 +1,3 @@
// @ts-nocheck
import {
getLoginTypeLabel,
normalizeLoginType,
@@ -72,11 +70,26 @@ import {
registerTencentBrowserSession,
resetTencentSessionAutoClose,
} from './session-service-store.js'
import type { TencentBrowserSession } from './session-service-store.js'
type JsonObject = Record<string, any>
type IncludeQrImageOptions = {
includeQrImage?: boolean
}
type CloseAllSessionsOptions = {
markClosed?: boolean
}
type CloseManagedSessionOptions = {
markClosed: boolean
}
export { resolveTencentSessionExpiresAt }
export { REDEEM_SUCCESS_AUTO_CLOSE_MS, warmupTencentBrowser }
export async function createTencentBrowserSession(payload = {}) {
export async function createTencentBrowserSession(payload: JsonObject = {}): Promise<JsonObject> {
await cleanupExpiredTencentBrowserSessions({ closeSession: closeManagedSession })
const loginType = normalizeLoginType(payload.loginType || payload.qrType)
@@ -120,15 +133,18 @@ export async function createTencentBrowserSession(payload = {}) {
}
}
export async function getTencentBrowserSession(sessionId, options = {}) {
export async function getTencentBrowserSession(
sessionId: unknown,
options: IncludeQrImageOptions = {},
): Promise<JsonObject> {
return refreshTencentBrowserSession(sessionId, options)
}
export async function getTencentBrowserSessionSummary(sessionId) {
export async function getTencentBrowserSessionSummary(sessionId: unknown): Promise<JsonObject> {
return refreshTencentBrowserSession(sessionId, { includeQrImage: false })
}
export async function reloadTencentBrowserSession(sessionId) {
export async function reloadTencentBrowserSession(sessionId: unknown): Promise<JsonObject> {
await cleanupExpiredTencentBrowserSessions({ closeSession: closeManagedSession })
const session = await getRequiredSession(sessionId)
@@ -170,7 +186,10 @@ export async function reloadTencentBrowserSession(sessionId) {
}
}
export async function refreshTencentBrowserSession(sessionId, { includeQrImage = true } = {}) {
export async function refreshTencentBrowserSession(
sessionId: unknown,
{ includeQrImage = true }: IncludeQrImageOptions = {},
): Promise<JsonObject> {
await cleanupExpiredTencentBrowserSessions({ closeSession: closeManagedSession })
const session = await getRequiredSession(sessionId)
@@ -255,7 +274,7 @@ export async function refreshTencentBrowserSession(sessionId, { includeQrImage =
}
}
export async function redeemTencentBrowserSession(sessionId, payload = {}) {
export async function redeemTencentBrowserSession(sessionId: unknown, payload: JsonObject = {}): Promise<JsonObject> {
const session = await getRequiredSession(sessionId)
const code = String(payload.code || payload.sCdKey || '').trim()
const maxAttempts = Math.max(1, Math.min(Number(payload.maxAttempts || 6), 12))
@@ -282,7 +301,8 @@ export async function redeemTencentBrowserSession(sessionId, payload = {}) {
session,
code,
maxAttempts,
ensureLoggedInPresentation,
ensureLoggedInPresentation: (redeemSession, options) =>
ensureLoggedInPresentation(redeemSession as any, options as any),
ensureActivityInfoReady,
fillRedeemCodeInBrowser,
capturePageCaptchaForOcr: (page, options) =>
@@ -307,7 +327,7 @@ export async function redeemTencentBrowserSession(sessionId, payload = {}) {
}
}
export async function closeTencentBrowserSession(sessionId) {
export async function closeTencentBrowserSession(sessionId: unknown): Promise<JsonObject> {
const session = await getRequiredSession(sessionId)
await closeManagedSession(session, { markClosed: true })
return {
@@ -316,7 +336,9 @@ export async function closeTencentBrowserSession(sessionId) {
}
}
export async function closeAllTencentBrowserSessions({ markClosed = true } = {}) {
export async function closeAllTencentBrowserSessions(
{ markClosed = true }: CloseAllSessionsOptions = {},
): Promise<JsonObject> {
const activeSessions = listActiveTencentBrowserSessions()
await Promise.all(activeSessions.map((session) => closeManagedSession(session, { markClosed })))
@@ -326,7 +348,7 @@ export async function closeAllTencentBrowserSessions({ markClosed = true } = {})
}
}
export async function getTencentBrowserSessionScreenshotPath(sessionId) {
export async function getTencentBrowserSessionScreenshotPath(sessionId: unknown): Promise<string> {
const session = await getRequiredSession(sessionId)
const screenshotPath = session.lastRedeem?.screenshotPath
@@ -340,7 +362,7 @@ export async function getTencentBrowserSessionScreenshotPath(sessionId) {
return screenshotPath
}
export async function getTencentBrowserSessionReviewScreenshotPath(sessionId) {
export async function getTencentBrowserSessionReviewScreenshotPath(sessionId: unknown): Promise<string> {
const session = await getRequiredSession(sessionId)
const screenshotPath = session.lastReview?.screenshotPath
@@ -354,11 +376,14 @@ export async function getTencentBrowserSessionReviewScreenshotPath(sessionId) {
return screenshotPath
}
async function persistSessionState(session) {
async function persistSessionState(session: TencentBrowserSession): Promise<void> {
await persistStoredSessionState(session, { buildSessionPayload })
}
function buildSessionPayload(session, { includeQrImage = true } = {}) {
function buildSessionPayload(
session: TencentBrowserSession,
{ includeQrImage = true }: IncludeQrImageOptions = {},
): JsonObject {
return buildSessionPayloadBase(session, {
activityUrl: ACTIVITY_URL,
includeQrImage,
@@ -367,11 +392,14 @@ function buildSessionPayload(session, { includeQrImage = true } = {}) {
})
}
async function fillRedeemCodeInBrowser(page, code) {
async function fillRedeemCodeInBrowser(page: any, code: string): Promise<void> {
await prepareRedeemCodeFill(page, code, { ensureActivityInfoReady })
}
async function closeManagedSession(session, { markClosed }) {
async function closeManagedSession(
session: TencentBrowserSession,
{ markClosed }: CloseManagedSessionOptions,
): Promise<void> {
await closeStoredSession(session, {
markClosed,
persistSessionState,