Files
sub2api/frontend/src/views/user/BatchImageGuideView.vue
T

2564 lines
106 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<AppLayout>
<TablePageLayout>
<template #filters>
<div class="flex flex-col gap-3">
<div class="flex flex-col gap-3 2xl:flex-row 2xl:items-center 2xl:justify-between">
<div class="grid w-full grid-cols-1 gap-3 sm:grid-cols-2 lg:grid-cols-[260px_160px_144px_152px] 2xl:w-auto">
<div class="min-w-0">
<SearchInput
v-model="filters.taskName"
placeholder="搜索任务名称"
class="w-full"
@search="applyFilters"
/>
</div>
<Select v-model="filters.apiKeyId" :options="apiKeyFilterOptions" class="w-full" @change="applyFilters" />
<Select v-model="filters.status" :options="statusFilterOptions" class="w-full" @change="applyFilters" />
<Select v-model="filters.downloaded" :options="downloadFilterOptions" class="w-full" @change="applyFilters" />
</div>
<div class="flex flex-wrap items-center justify-start gap-2 sm:justify-end 2xl:flex-shrink-0">
<button type="button" class="btn btn-secondary" :disabled="loadingJobs" @click="resetFilters">
重置
</button>
<button type="button" class="btn btn-secondary" :disabled="loadingKeys || loadingJobs" :title="'刷新'" @click="refreshPage">
<Icon name="refresh" size="md" :class="loadingKeys || loadingJobs ? 'animate-spin' : ''" />
</button>
<button type="button" class="btn btn-secondary" @click="showGuideModal = true">
<Icon name="book" size="md" class="mr-2" />
使用说明
</button>
<button type="button" class="btn btn-primary" @click="openCreateModal">
<Icon name="plus" size="md" class="mr-2" />
创建批量任务
</button>
</div>
</div>
<div
v-if="selectedJobIds.size"
class="flex flex-wrap items-center justify-between gap-3 rounded-lg border border-gray-200 bg-white px-3 py-2 shadow-sm dark:border-dark-700 dark:bg-dark-800"
>
<span class="text-sm text-gray-600 dark:text-gray-300">
已选择 <span class="font-medium text-gray-900 dark:text-white">{{ selectedJobIds.size }}</span> 个任务
</span>
<div class="flex flex-wrap items-center gap-2">
<button
type="button"
class="btn btn-secondary btn-sm"
:disabled="bulkDownloading || selectedDownloadableRows.length === 0"
@click="downloadSelectedJobs"
>
<Icon :name="bulkDownloading ? 'refresh' : 'download'" size="sm" class="mr-1.5" :class="bulkDownloading ? 'animate-spin' : ''" />
下载选中
</button>
<button
type="button"
class="btn btn-secondary btn-sm text-red-600 hover:text-red-700 dark:text-red-400"
:disabled="bulkDeleting"
@click="deleteSelectedJobs"
>
<Icon :name="bulkDeleting ? 'refresh' : 'trash'" size="sm" class="mr-1.5" :class="bulkDeleting ? 'animate-spin' : ''" />
删除记录
</button>
</div>
</div>
</div>
</template>
<template #table>
<DataTable
:columns="columns"
:data="visibleBatchJobs"
:loading="loadingKeys || loadingJobs"
:expandable-actions="false"
row-key="id"
>
<template #header-select>
<input
type="checkbox"
class="h-4 w-4 rounded border-gray-300 text-primary-600 focus:ring-primary-500"
:checked="allVisibleSelected"
:indeterminate="someVisibleSelected"
@change="toggleAllVisible(($event.target as HTMLInputElement).checked)"
/>
</template>
<template #cell-select="{ row }">
<input
type="checkbox"
class="h-4 w-4 rounded border-gray-300 text-primary-600 focus:ring-primary-500"
:checked="selectedJobIds.has(row.id)"
@change="toggleJobSelection(row.id, ($event.target as HTMLInputElement).checked)"
@click.stop
/>
</template>
<template #cell-id="{ row }">
<div class="flex w-[220px] items-start gap-1" :class="row.is_child ? 'pl-6' : ''">
<button
v-if="row.child_count > 0 && !row.is_child"
type="button"
class="mt-1 flex h-6 w-6 flex-shrink-0 items-center justify-center rounded-md text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500/30 dark:text-gray-400 dark:hover:bg-dark-700 dark:hover:text-white"
:title="expandedParentIds.has(row.id) ? '收起子任务' : `展开 ${row.child_count} 个子任务`"
@click.stop="toggleChildRows(row.id)"
>
<Icon :name="expandedParentIds.has(row.id) ? 'chevronDown' : 'chevronRight'" size="xs" />
</button>
<span v-else class="w-6 flex-shrink-0" />
<button type="button" class="min-w-0 flex-1 rounded-lg py-1 text-left transition-colors hover:bg-gray-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500/30 dark:hover:bg-dark-700" @click="selectJob(row.id)">
<span
class="flex min-w-0 items-center gap-2 text-sm font-medium"
:class="row.task_name ? 'text-gray-900 dark:text-white' : 'text-gray-500 dark:text-gray-400'"
>
<span class="min-w-0 truncate">{{ row.task_name || defaultTaskName(row.created_at) }}</span>
<span v-if="row.child_count > 0 && !row.is_child" class="flex-shrink-0 rounded-full bg-gray-100 px-2 py-0.5 text-xs font-normal text-gray-600 dark:bg-dark-700 dark:text-gray-300">
{{ row.child_count }} 子任务
</span>
<span v-if="row.is_child" class="flex-shrink-0 rounded-full bg-amber-50 px-2 py-0.5 text-xs font-normal text-amber-700 dark:bg-amber-900/20 dark:text-amber-300">
子任务
</span>
</span>
<span class="mt-1 flex flex-wrap items-center gap-x-2 gap-y-1 text-xs text-gray-500 dark:text-gray-400">
<span>{{ formatDate(row.created_at) }}</span>
</span>
</button>
</div>
</template>
<template #cell-model="{ row }">
<div class="mx-auto max-w-[180px] text-center">
<p class="truncate text-sm text-gray-700 dark:text-gray-300" :title="row.model">{{ row.model }}</p>
</div>
</template>
<template #cell-api_key_name="{ value }">
<span class="block truncate text-center text-sm text-gray-700 dark:text-gray-300">
{{ value || '未记录' }}
</span>
</template>
<template #cell-status="{ row }">
<div class="flex justify-center">
<span :class="statusBadgeClass(displayJob(row))" class="badge">
{{ statusLabel(displayJob(row)) }}
</span>
</div>
</template>
<template #cell-counts="{ row }">
<div class="flex items-center justify-center gap-2 text-sm tabular-nums">
<span class="text-emerald-600 dark:text-emerald-300">{{ displayJob(row).success_count }}</span>
<span class="text-gray-300 dark:text-dark-500">/</span>
<span :class="displayJob(row).fail_count > 0 ? 'text-red-600 dark:text-red-300' : 'text-gray-400 dark:text-gray-500'">{{ displayJob(row).fail_count }}</span>
<span class="text-xs text-gray-400 dark:text-gray-500">共 {{ displayJob(row).item_count }}</span>
</div>
</template>
<template #cell-cost="{ row }">
<span class="block text-center text-sm text-gray-700 dark:text-gray-300">
{{ costLabel(displayJob(row)) }}
</span>
</template>
<template #cell-downloaded="{ row }">
<span class="block text-center text-sm" :class="row.downloaded_at ? 'text-emerald-700 dark:text-emerald-300' : 'text-gray-500 dark:text-gray-400'">
{{ row.downloaded_at ? formatDate(row.downloaded_at) : '未下载' }}
</span>
</template>
<template #cell-actions="{ row }">
<div class="flex items-center justify-center gap-1">
<button
type="button"
class="batch-row-action flex flex-col items-center gap-0.5 rounded-lg p-1.5 text-gray-500 transition-colors hover:bg-gray-100 hover:text-primary-600 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500/30 dark:hover:bg-dark-700 dark:hover:text-primary-400"
title="查看详情"
@click="selectJob(row.id)"
>
<Icon name="eye" size="sm" />
<span class="text-xs">查看</span>
</button>
<button
type="button"
class="batch-row-action flex flex-col items-center gap-0.5 rounded-lg p-1.5 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500/30"
:class="canDownload(row) ? 'text-gray-500 hover:bg-green-50 hover:text-green-600 dark:hover:bg-green-900/20 dark:hover:text-green-400' : 'text-gray-300 dark:text-dark-500'"
:disabled="!canDownload(row) || downloading"
title="下载 ZIP"
@click="downloadJob(row)"
>
<Icon
:name="isDownloadingJob(row.id) ? 'refresh' : 'download'"
size="sm"
:class="isDownloadingJob(row.id) ? 'animate-spin' : ''"
/>
<span class="text-xs">下载</span>
</button>
<div v-if="canRetry(row) || canDeleteRecord(row)">
<button
type="button"
class="batch-row-action flex flex-col items-center gap-0.5 rounded-lg p-1.5 text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500/30 dark:hover:bg-dark-700 dark:hover:text-white"
:class="{ 'bg-gray-100 text-gray-900 dark:bg-dark-700 dark:text-white': openMoreJobId === row.id }"
title="更多操作"
@click.stop="toggleMoreMenu(row, $event)"
>
<Icon name="more" size="sm" />
<span class="text-xs">更多</span>
</button>
</div>
</div>
</template>
<template #empty>
<div class="flex min-h-[260px] flex-col items-center justify-center py-6 md:min-h-[300px]">
<Icon name="sparkles" size="xl" class="mb-4 h-12 w-12 text-gray-400 dark:text-dark-500" />
<p class="text-lg font-medium text-gray-900 dark:text-gray-100">暂无批量任务</p>
<p class="mt-1 text-sm text-gray-500 dark:text-gray-400">
点击右上角创建批量任务。
</p>
</div>
</template>
</DataTable>
</template>
<template #pagination>
<div
v-if="visibleBatchJobs.length > 0 || pagination.page > 1"
class="flex flex-col gap-3 border-t border-gray-200 bg-white px-4 py-3 dark:border-dark-700 dark:bg-dark-800 sm:flex-row sm:items-center sm:justify-between sm:px-6"
>
<div class="flex flex-wrap items-center gap-3 text-sm text-gray-700 dark:text-gray-300">
<span>
第 <span class="font-medium">{{ pagination.page }}</span> 页
</span>
<span>
本页 <span class="font-medium">{{ visibleBatchJobs.length }}</span> 条
</span>
<div class="flex items-center gap-2">
<span>每页</span>
<Select
v-model="pagination.page_size"
:options="batchPageSizeOptions"
class="w-24"
@change="handlePageSizeChange"
/>
</div>
</div>
<div class="flex items-center justify-end gap-2">
<button
type="button"
class="btn btn-secondary btn-sm"
:disabled="pagination.page <= 1 || loadingJobs"
@click="handlePageChange(pagination.page - 1)"
>
<Icon name="chevronLeft" size="sm" class="mr-1" />
上一页
</button>
<button
type="button"
class="btn btn-secondary btn-sm"
:disabled="!pagination.has_more || loadingJobs"
@click="handlePageChange(pagination.page + 1)"
>
下一页
<Icon name="chevronRight" size="sm" class="ml-1" />
</button>
</div>
</div>
</template>
</TablePageLayout>
<Teleport to="body">
<div
v-if="openMoreJobId"
class="fixed z-[9999] w-44 overflow-hidden rounded-xl bg-white py-1 text-sm shadow-lg ring-1 ring-black/5 dark:bg-dark-800 dark:ring-white/10"
:style="moreMenuStyle"
@click.stop
>
<template v-for="job in batchJobs" :key="job.id">
<template v-if="job.id === openMoreJobId">
<button
v-if="canRetry(job)"
type="button"
class="flex w-full items-center gap-2 px-3 py-2 text-left text-gray-700 transition-colors hover:bg-amber-50 hover:text-amber-700 disabled:opacity-60 dark:text-gray-200 dark:hover:bg-amber-900/20 dark:hover:text-amber-300"
:disabled="retryingBatchId === job.id"
@click="retryFailedJob(job)"
>
<Icon name="refresh" size="sm" :class="retryingBatchId === job.id ? 'animate-spin' : ''" />
重试失败项
</button>
<button
v-if="canDeleteRecord(job)"
type="button"
class="flex w-full items-center gap-2 px-3 py-2 text-left text-red-600 transition-colors hover:bg-red-50 disabled:opacity-60 dark:text-red-400 dark:hover:bg-red-900/20"
:disabled="deletingBatchId === job.id"
@click="deleteJob(job)"
>
<Icon :name="deletingBatchId === job.id ? 'refresh' : 'trash'" size="sm" :class="deletingBatchId === job.id ? 'animate-spin' : ''" />
删除记录
</button>
</template>
</template>
</div>
</Teleport>
<Teleport to="body">
<div
v-if="promptPopover.visible"
class="batch-prompt-popover fixed z-[9999] rounded-lg border border-gray-200 bg-white p-3 text-sm text-gray-800 shadow-xl ring-1 ring-black/5 dark:border-dark-700 dark:bg-dark-900 dark:text-gray-100 dark:ring-white/10"
:style="promptPopover.style"
@mouseenter="cancelPromptPopoverClose"
@mouseleave="schedulePromptPopoverClose"
>
<div class="mb-2 flex items-center justify-between gap-3">
<span class="text-xs font-medium text-gray-500 dark:text-gray-400">完整 Prompt</span>
<button
type="button"
class="rounded-md px-2 py-1 text-xs font-medium text-primary-600 transition-colors hover:bg-primary-50 focus:outline-none focus-visible:ring-2 focus-visible:ring-primary-500/30 dark:text-primary-300 dark:hover:bg-primary-900/20"
@click="copyPromptPopover"
>
复制
</button>
</div>
<p class="max-h-48 overflow-y-auto whitespace-pre-wrap break-words leading-6 selection:bg-primary-100 selection:text-primary-900 dark:selection:bg-primary-900/60 dark:selection:text-primary-100">
{{ promptPopover.text }}
</p>
</div>
</Teleport>
<BaseDialog :show="!!currentJob" title="任务详情" width="extra-wide" @close="closeDetail">
<div v-if="currentJob" class="space-y-4">
<div class="rounded-lg border border-gray-200 bg-gray-50/70 px-4 py-3 dark:border-dark-700 dark:bg-dark-900/40">
<div class="grid gap-x-6 gap-y-3 sm:grid-cols-2 lg:grid-cols-4">
<div class="min-w-0 text-center">
<p class="text-xs text-gray-500 dark:text-gray-400">状态</p>
<div class="mt-1 flex justify-center">
<span :class="statusBadgeClass(currentDisplayJob || currentJob)" class="badge whitespace-nowrap">
{{ statusLabel(currentDisplayJob || currentJob) }}
</span>
</div>
</div>
<div class="min-w-0 text-center">
<p class="text-xs text-gray-500 dark:text-gray-400">{{ hasChildJobs(currentJob.id) ? '汇总结果' : '结果' }}</p>
<p class="mt-1 flex items-center justify-center gap-2 font-medium tabular-nums">
<span class="text-emerald-600 dark:text-emerald-300">{{ (currentDisplayJob || currentJob).success_count }}</span>
<span class="text-gray-300 dark:text-dark-500">/</span>
<span :class="(currentDisplayJob || currentJob).fail_count > 0 ? 'text-red-600 dark:text-red-300' : 'text-gray-400 dark:text-gray-500'">{{ (currentDisplayJob || currentJob).fail_count }}</span>
</p>
</div>
<div class="min-w-0 text-center">
<p class="text-xs text-gray-500 dark:text-gray-400">费用</p>
<p class="mt-1 truncate font-medium text-gray-900 dark:text-white">{{ costLabel(currentDisplayJob || currentJob) }}</p>
</div>
<div class="min-w-0 text-center">
<p class="text-xs text-gray-500 dark:text-gray-400">下载状态</p>
<p class="mt-1 truncate font-medium text-gray-900 dark:text-white">
{{ currentJob.downloaded_at ? formatDate(currentJob.downloaded_at) : '未下载' }}
</p>
</div>
</div>
</div>
<div class="flex flex-wrap items-center justify-between gap-3">
<h3 class="text-sm font-semibold text-gray-900 dark:text-white">明细</h3>
<button type="button" class="btn btn-secondary btn-sm" :disabled="refreshing || loadingItems" @click="refreshDetail">
<Icon name="refresh" size="sm" class="mr-1.5" :class="refreshing || loadingItems ? 'animate-spin' : ''" />
刷新
</button>
</div>
<div v-if="items.length" class="overflow-x-auto rounded-lg border border-gray-200 bg-white dark:border-dark-700 dark:bg-dark-900">
<table class="w-full min-w-[860px] table-fixed divide-y divide-gray-200 text-sm dark:divide-dark-700">
<colgroup>
<col class="w-[18%]" />
<col class="w-[34%]" />
<col class="w-[12%]" />
<col class="w-[10%]" />
<col class="w-[26%]" />
</colgroup>
<thead class="bg-gray-50 dark:bg-dark-800/80">
<tr>
<th class="px-3 py-3 text-center text-sm font-medium text-gray-500 dark:text-gray-400">Custom ID</th>
<th class="px-3 py-3 text-left text-sm font-medium text-gray-500 dark:text-gray-400">Prompt</th>
<th class="px-3 py-3 text-center text-sm font-medium text-gray-500 dark:text-gray-400">状态</th>
<th class="px-3 py-3 text-center text-sm font-medium text-gray-500 dark:text-gray-400">预览</th>
<th class="px-3 py-3 text-center text-sm font-medium text-gray-500 dark:text-gray-400">结果</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-100 dark:divide-dark-700">
<tr
v-for="item in items"
:key="itemPreviewKey(item)"
class="align-middle"
:class="detailItemRowClass(item)"
>
<td class="px-3 py-2.5 text-center">
<span
class="block min-w-0 truncate font-mono text-sm"
:class="isRecoveredOriginalFailure(item) ? 'text-gray-400 dark:text-gray-500' : 'text-gray-900 dark:text-white'"
:title="item.custom_id"
>
{{ item.custom_id }}
</span>
</td>
<td class="px-3 py-2.5 text-left" :class="isRecoveredOriginalFailure(item) ? 'text-gray-400 dark:text-gray-500' : 'text-gray-700 dark:text-gray-300'">
<div
class="batch-prompt-trigger cursor-default truncate rounded px-1 text-sm leading-6 focus:outline-none"
tabindex="0"
@pointerenter="schedulePromptPopoverOpen($event, item.prompt_preview || '-')"
@pointerleave="schedulePromptPopoverClose"
@mouseenter="schedulePromptPopoverOpen($event, item.prompt_preview || '-')"
@mouseleave="schedulePromptPopoverClose"
@click="showPromptPopover($event, item.prompt_preview || '-')"
@focus="showPromptPopover($event, item.prompt_preview || '-')"
@focusin="showPromptPopover($event, item.prompt_preview || '-')"
@blur="schedulePromptPopoverClose"
>
{{ item.prompt_preview || '-' }}
</div>
</td>
<td class="px-3 py-2.5 text-center">
<span :class="itemDisplayStatusBadgeClass(item)" class="badge max-w-full truncate whitespace-nowrap" :title="itemDisplayStatusLabel(item)">
{{ itemDisplayStatusLabel(item) }}
</span>
</td>
<td class="px-3 py-2.5 text-center">
<div class="mx-auto h-12 w-12 overflow-hidden rounded-md border border-gray-200 bg-gray-50 dark:border-dark-700 dark:bg-dark-800">
<button
v-if="itemPreviewUrls[itemPreviewKey(item)] && !previewErrorIds.has(itemPreviewKey(item))"
type="button"
class="block h-full w-full overflow-hidden"
:title="`放大压缩预览 ${item.custom_id}`"
@click="openImagePreview(item)"
>
<img
:src="itemPreviewUrls[itemPreviewKey(item)]"
class="h-full w-full object-cover"
alt=""
@error="handlePreviewError(itemPreviewKey(item))"
/>
</button>
<button
v-else-if="canLoadItemPreview(item)"
type="button"
class="flex h-full w-full items-center justify-center text-gray-500 transition-colors hover:bg-gray-100 hover:text-primary-600 disabled:cursor-wait disabled:opacity-70 dark:text-gray-400 dark:hover:bg-dark-700"
:disabled="previewLoadingIds.has(itemPreviewKey(item))"
:title="previewErrorIds.has(itemPreviewKey(item)) ? '重新加载压缩预览' : '加载压缩预览'"
@click="loadItemPreview(item)"
>
<Icon :name="previewLoadingIds.has(itemPreviewKey(item)) ? 'refresh' : 'eye'" size="sm" :class="previewLoadingIds.has(itemPreviewKey(item)) ? 'animate-spin' : ''" />
</button>
<div v-else class="flex h-full w-full items-center justify-center text-gray-400" :title="item.image_count > 0 ? '不可预览' : '无图片'">
<Icon name="document" size="sm" />
</div>
</div>
</td>
<td class="px-3 py-2.5 text-center">
<span
class="inline-flex max-w-full items-center justify-center truncate rounded-md px-2.5 py-1 text-xs font-medium leading-5 ring-1 ring-inset"
:class="itemResultClass(item)"
:title="itemResultLabel(item)"
>
{{ itemResultLabel(item) }}
</span>
</td>
</tr>
</tbody>
</table>
</div>
<div v-else class="rounded-lg border border-dashed border-gray-200 py-10 text-center dark:border-dark-700">
<Icon name="refresh" size="lg" class="mx-auto mb-3 text-gray-400" :class="loadingItems ? 'animate-spin' : ''" />
<p class="text-sm font-medium text-gray-700 dark:text-gray-200">
{{ loadingItems ? '正在加载明细...' : '暂无明细' }}
</p>
<p v-if="!loadingItems" class="mt-1 text-sm text-gray-500 dark:text-gray-400">
排队或生成中的任务会先显示已提交的 prompt,结果整理完成后会更新图片状态。
</p>
</div>
</div>
<template #footer>
<div class="flex justify-end gap-3">
<button type="button" class="btn btn-secondary" :disabled="!currentJob || !canCancel(currentJob) || cancelling" @click="cancelSelected">
<Icon v-if="cancelling" name="refresh" size="sm" class="mr-2 animate-spin" />
取消任务
</button>
<button
v-if="currentJob && currentDisplayJob && canRetry(currentDisplayJob)"
type="button"
class="btn btn-secondary inline-flex min-w-[116px] items-center justify-center"
:disabled="retryingBatchId === currentJob.id"
@click="retrySelected"
>
<Icon name="refresh" size="sm" class="mr-2" :class="currentJob && retryingBatchId === currentJob.id ? 'animate-spin' : ''" />
重试失败项
</button>
<button
type="button"
class="btn btn-primary inline-flex min-w-[112px] items-center justify-center"
:disabled="!currentJob || !canDownload(currentJob) || downloading"
@click="downloadSelected"
>
<Icon
:name="currentJob && isDownloadingJob(currentJob.id) ? 'refresh' : 'download'"
size="sm"
class="mr-2"
:class="currentJob && isDownloadingJob(currentJob.id) ? 'animate-spin' : ''"
/>
下载 ZIP
</button>
</div>
</template>
</BaseDialog>
<BaseDialog :show="!!previewImageItem" :title="previewImageItem?.custom_id || '图片预览'" width="extra-wide" :z-index="60" @close="closeImagePreview">
<div class="space-y-3">
<div class="rounded-lg border border-amber-200 bg-amber-50 px-3 py-2 text-sm text-amber-900 dark:border-amber-800 dark:bg-amber-950/30 dark:text-amber-100">
当前显示的是浏览器本地缓存的压缩缩略图,清晰度会有影响;需要查看原图请下载 ZIP。
</div>
<div class="flex min-h-[420px] items-center justify-center rounded-lg bg-gray-50 p-4 dark:bg-dark-900">
<img
v-if="previewImageUrl"
:src="previewImageUrl"
class="max-h-[70vh] max-w-full rounded-md object-contain"
:alt="previewImageItem?.custom_id || ''"
/>
</div>
</div>
</BaseDialog>
<BaseDialog :show="showCreateModal" title="创建批量任务" width="wide" @close="closeCreateModal">
<form class="space-y-5" @submit.prevent="submitJob">
<div class="grid gap-4 md:grid-cols-2">
<div class="md:col-span-2">
<label class="input-label">任务名称</label>
<input
v-model="form.taskName"
type="text"
maxlength="255"
class="input"
placeholder="不填写则默认使用当前时间"
/>
</div>
<div class="md:col-span-2">
<label class="input-label">API Key</label>
<select v-model.number="form.apiKeyId" class="input" :disabled="loadingKeys">
<option :value="0">{{ loadingKeys ? '加载 API Key 中...' : '请选择 Gemini API Key' }}</option>
<option v-for="key in geminiApiKeys" :key="key.id" :value="key.id">
{{ key.name }} · {{ key.group?.name || 'Gemini' }}
</option>
</select>
<p v-if="!loadingKeys && geminiApiKeys.length === 0" class="input-hint text-amber-600 dark:text-amber-400">
当前没有可用于批量生图的 Gemini API Key。请先创建并绑定已开启批量生图的 Gemini 分组。
</p>
</div>
<div>
<label class="input-label">模型</label>
<select v-model="form.model" class="input" :disabled="loadingModels || availableBatchImageModels.length === 0">
<option v-if="loadingModels" value="">{{ batchImageText('loadingModels') }}</option>
<option v-else-if="availableBatchImageModels.length === 0" value="">{{ batchImageText('noModels') }}</option>
<option v-for="model in availableBatchImageModels" :key="model.value" :value="model.value">
{{ model.label }}
</option>
</select>
<p v-if="modelLoadError" class="input-hint text-amber-600 dark:text-amber-400">
{{ modelLoadError }}
</p>
<p v-else-if="selectedApiKey && !loadingModels && availableBatchImageModels.length === 0" class="input-hint text-amber-600 dark:text-amber-400">
{{ batchImageText('noModelsHint') }}
</p>
</div>
<div>
<label class="input-label">图片尺寸</label>
<div class="input flex items-center bg-gray-50 text-gray-600 dark:bg-dark-900 dark:text-gray-300">
1K
</div>
<p class="input-hint">当前批量任务固定按 1K 图片提交。</p>
</div>
<div>
<label class="input-label">输出格式</label>
<select v-model="form.responseMimeType" class="input">
<option value="image/png">PNG</option>
<option value="image/jpeg">JPEG</option>
<option value="image/webp">WebP</option>
</select>
</div>
<div>
<label class="input-label">Prompt 数量</label>
<div class="input flex items-center bg-gray-50 text-gray-600 dark:bg-dark-900 dark:text-gray-300">
{{ parsedItems.length }} 条
</div>
</div>
</div>
<div class="space-y-3">
<div class="flex items-center justify-between gap-3">
<label class="input-label mb-0">Prompt</label>
<span class="text-xs text-gray-500 dark:text-gray-400">已添加 {{ promptRows.length }} 条</span>
</div>
<div class="rounded-lg border border-gray-200 p-3 dark:border-dark-700">
<div class="grid gap-3 md:grid-cols-[180px_minmax(0,1fr)_96px] md:items-start">
<input
v-model="customIdDraft"
type="text"
maxlength="255"
class="input"
placeholder="Custom ID 可选"
/>
<textarea
v-model="promptDraft"
class="min-h-[120px] w-full resize-y rounded-md border border-gray-300 px-3 py-2 text-sm outline-none focus:border-primary-500 focus:ring-2 focus:ring-primary-100 dark:border-dark-600 dark:bg-dark-900 dark:text-gray-100 dark:focus:border-primary-500 dark:focus:ring-primary-900/40"
placeholder="输入一整段 prompt,点击添加后会进入下方列表"
/>
<button type="button" class="btn btn-secondary h-10 justify-center" :disabled="!promptDraft.trim()" @click="addPromptRow">
<Icon name="plus" size="sm" class="mr-1.5" />
添加
</button>
</div>
</div>
<div v-if="promptRows.length" class="overflow-hidden rounded-lg border border-gray-200 dark:border-dark-700">
<div
v-for="(row, index) in promptRows"
:key="row.localId"
class="flex items-center gap-3 border-b border-gray-100 px-3 py-2 last:border-b-0 dark:border-dark-700"
>
<span class="w-20 flex-shrink-0 font-mono text-xs text-gray-500 dark:text-gray-400">{{ row.custom_id }}</span>
<p class="min-w-0 flex-1 truncate text-sm text-gray-800 dark:text-gray-100">{{ row.prompt }}</p>
<button type="button" class="btn-ghost btn-icon flex-shrink-0 text-red-600 hover:bg-red-50 dark:text-red-400 dark:hover:bg-red-900/20" title="删除" @click="removePromptRow(index)">
<Icon name="trash" size="sm" />
</button>
</div>
</div>
<div v-else class="rounded-lg border border-dashed border-gray-200 px-3 py-6 text-center text-sm text-gray-500 dark:border-dark-700 dark:text-gray-400">
还没有添加 prompt。
</div>
</div>
<div class="rounded-lg border border-amber-200 bg-amber-50 p-3 text-sm leading-6 text-amber-900 dark:border-amber-800 dark:bg-amber-950/30 dark:text-amber-100">
取消任务会请求上游取消;已被系统索引为成功的图片仍会按成功项结算扣费,其余冻结金额会释放。
</div>
<div v-if="submitting" class="rounded-lg border border-sky-200 bg-sky-50 p-3 text-sm leading-6 text-sky-800 dark:border-sky-800 dark:bg-sky-950/30 dark:text-sky-100">
正在创建上游批量任务,通常需要几秒,请不要重复提交。
</div>
</form>
<template #footer>
<div class="flex justify-end gap-3">
<button type="button" class="btn btn-secondary" :disabled="submitting" @click="closeCreateModal">取消</button>
<button type="button" class="btn btn-primary inline-flex min-w-[120px] justify-center" :disabled="submitting || loadingModels || (parsedItems.length === 0 && !promptDraft.trim()) || !selectedApiKey || !form.model" @click="submitJob">
<Icon v-if="submitting" name="refresh" size="sm" class="mr-2 animate-spin" />
{{ submitting ? '提交中...' : '提交任务' }}
</button>
</div>
</template>
</BaseDialog>
<BaseDialog :show="showGuideModal" title="批量生图使用说明" width="wide" @close="showGuideModal = false">
<div class="space-y-5">
<section class="space-y-3">
<h3 class="text-sm font-semibold text-gray-900 dark:text-white">当前界面如何使用</h3>
<div class="rounded-lg border border-gray-200 bg-gray-50 p-3 text-sm leading-6 text-gray-700 dark:border-dark-700 dark:bg-dark-900/50 dark:text-gray-200">
<p>1. 选择已开启批量生图的 Gemini API Key,模型列表会按该 Key 所属分组可用模型展示。</p>
<p>2. 任务名称可以留空,提交时会自动使用当前时间;Prompt 需要一条条添加到列表里。</p>
<p>3. 提交后任务会先排队,明细会展示已提交的 Prompt;图片预览默认不加载,点击明细里的预览按钮才会加载单张图。</p>
<p>4. 完成后可以下载 ZIP;部分失败时,更多菜单里可以只重试失败项。</p>
</div>
</section>
<section class="space-y-3">
<div class="flex flex-wrap items-center justify-between gap-3">
<h3 class="text-sm font-semibold text-gray-900 dark:text-white">给 Codex 的 Skill 说明</h3>
<p class="text-xs text-gray-500 dark:text-gray-400">用于告诉 Codex 如何代替用户整理 prompt、提交任务和下载结果。</p>
</div>
<textarea
:value="agentInstruction"
readonly
class="min-h-[420px] w-full resize-y rounded-md border border-gray-200 bg-gray-50 p-4 font-mono text-sm leading-6 text-gray-800 outline-none focus:border-primary-400 focus:ring-2 focus:ring-primary-100 dark:border-dark-600 dark:bg-dark-900 dark:text-gray-100 dark:focus:border-primary-500 dark:focus:ring-primary-900/40"
/>
</section>
</div>
<template #footer>
<div class="flex justify-end gap-3">
<button type="button" class="btn btn-secondary" @click="showGuideModal = false">关闭</button>
<button type="button" class="btn btn-primary" @click="copyInstruction">
<Icon name="copy" size="sm" class="mr-2" />
复制说明
</button>
</div>
</template>
</BaseDialog>
</AppLayout>
</template>
<script setup lang="ts">
import { computed, onBeforeUnmount, onMounted, reactive, ref, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import AppLayout from '@/components/layout/AppLayout.vue'
import TablePageLayout from '@/components/layout/TablePageLayout.vue'
import DataTable from '@/components/common/DataTable.vue'
import BaseDialog from '@/components/common/BaseDialog.vue'
import Select, { type SelectOption } from '@/components/common/Select.vue'
import SearchInput from '@/components/common/SearchInput.vue'
import Icon from '@/components/icons/Icon.vue'
import { useClipboard } from '@/composables/useClipboard'
import { getPersistedPageSize, setPersistedPageSize } from '@/composables/usePersistedPageSize'
import { useAppStore } from '@/stores/app'
import { keysAPI } from '@/api'
import {
cancelBatchImageJob,
deleteBatchImageJobRecord,
downloadBatchImageZip,
getBatchImageItemContent,
getBatchImageJob,
listBatchImageJobs,
listBatchImageItems,
listBatchImageModels,
saveBlob,
submitBatchImageJob,
type BatchImageItem,
type BatchImageJob,
type BatchImageJobsListOptions,
type BatchImageStatus,
type BatchImageSubmitItem,
} from '@/api/batchImage'
import type { ApiKey } from '@/types'
import type { Column } from '@/components/common/types'
type BatchImageJobRow = Pick<BatchImageJob, 'id' | 'task_name' | 'parent_batch_id' | 'status' | 'model' | 'provider' | 'item_count' | 'success_count' | 'fail_count' | 'estimated_cost' | 'hold_amount' | 'actual_cost' | 'created_at' | 'downloaded_at'> & {
api_key_id: number
api_key_name: string
child_count: number
is_child?: boolean
}
type BatchImageDetailItem = BatchImageItem & {
batch_id: string
source_task_name: string
}
type PromptRow = {
localId: string
custom_id: string
prompt: string
}
type PreviewCacheRecord = {
key: string
blob: Blob
size: number
createdAt: number
lastAccessedAt: number
}
type PreviewImageSource = ImageBitmap | HTMLImageElement
const TERMINAL_STATUSES = new Set(['completed', 'failed', 'cancelled', 'output_deleted'])
const PREVIEW_CACHE_DB_NAME = 'sub2api-batch-image-preview-cache'
const PREVIEW_CACHE_STORE_NAME = 'thumbnails'
const PREVIEW_THUMBNAIL_MAX_EDGE = 360
const PREVIEW_THUMBNAIL_QUALITY = 0.72
const PREVIEW_CACHE_MAX_AGE_MS = 3 * 24 * 60 * 60 * 1000
const PREVIEW_CACHE_MAX_ENTRIES = 120
const PREVIEW_CACHE_MAX_BYTES = 48 * 1024 * 1024
const batchPageSizeOptions: SelectOption[] = [20, 50, 100].map(size => ({ value: size, label: String(size) }))
const appStore = useAppStore()
const { copyToClipboard } = useClipboard()
const { locale } = useI18n()
const columns: Column[] = [
{ key: 'select', label: '', sortable: false, class: 'w-12 text-center' },
{ key: 'id', label: '任务名称', sortable: false, class: 'w-[240px] max-w-[240px]' },
{ key: 'model', label: '模型', sortable: false, class: 'w-[180px] max-w-[180px] text-center' },
{ key: 'api_key_name', label: '提交密钥', sortable: false, class: 'w-40 max-w-40 text-center' },
{ key: 'status', label: '状态', sortable: false, class: 'w-28 text-center' },
{ key: 'counts', label: '结果', sortable: false, class: 'w-32 text-center' },
{ key: 'cost', label: '费用', sortable: false, class: 'w-36 text-center' },
{ key: 'downloaded', label: '下载状态', sortable: false, class: 'w-40 text-center' },
{ key: 'actions', label: '操作', sortable: false, class: 'w-40 text-center' },
]
const statusFilterOptions: SelectOption[] = [
{ value: '', label: '全部状态' },
{ value: 'queued', label: '排队中' },
{ value: 'running', label: '生成中' },
{ value: 'processing_results', label: '整理结果' },
{ value: 'settling', label: '结算中' },
{ value: 'completed', label: '已完成' },
{ value: 'failed', label: '失败' },
{ value: 'cancelled', label: '已取消' },
{ value: 'output_deleted', label: '结果已删除' },
]
const downloadFilterOptions: SelectOption[] = [
{ value: '', label: '全部下载状态' },
{ value: 'true', label: '已下载' },
{ value: 'false', label: '未下载' },
]
const form = reactive({
apiKeyId: 0,
taskName: '',
model: '',
responseMimeType: 'image/png',
})
const filters = reactive({
taskName: '',
apiKeyId: '',
status: '',
downloaded: '',
})
const pagination = reactive({
page: 1,
page_size: Math.min(getPersistedPageSize(20), 100),
has_more: false,
})
const apiKeys = ref<ApiKey[]>([])
const loadingKeys = ref(false)
const loadingJobs = ref(false)
const submitting = ref(false)
const refreshing = ref(false)
const cancelling = ref(false)
const downloading = ref(false)
const downloadingBatchId = ref('')
const retryingBatchId = ref('')
const bulkDownloading = ref(false)
const bulkDeleting = ref(false)
const deletingBatchId = ref('')
const loadingItems = ref(false)
const loadingModels = ref(false)
const showCreateModal = ref(false)
const showGuideModal = ref(false)
const currentJob = ref<BatchImageJob | null>(null)
const selectedBatchId = ref('')
const selectedBatchApiKeyId = ref(0)
const items = ref<BatchImageDetailItem[]>([])
const batchJobs = ref<BatchImageJobRow[]>([])
const selectedJobIds = ref(new Set<string>())
const expandedParentIds = ref(new Set<string>())
const promptRows = ref<PromptRow[]>([])
const promptDraft = ref('')
const customIdDraft = ref('')
const itemPreviewUrls = reactive<Record<string, string>>({})
const previewLoadingIds = ref(new Set<string>())
const previewErrorIds = ref(new Set<string>())
const previewImageItem = ref<BatchImageItem | null>(null)
const availableBatchImageModels = ref<Array<{ value: string; label: string }>>([])
const modelLoadError = ref('')
const openMoreJobId = ref('')
const moreMenuStyle = ref<Record<string, string>>({})
const promptPopover = reactive({
visible: false,
text: '',
style: {} as Record<string, string>,
})
let modelRequestSeq = 0
let pollTimer: ReturnType<typeof setInterval> | null = null
let previewCacheDBPromise: Promise<IDBDatabase | null> | null = null
let previewCacheCleanupTimer: ReturnType<typeof setInterval> | null = null
let promptPopoverCloseTimer: ReturnType<typeof setTimeout> | null = null
let promptPopoverOpenTimer: ReturnType<typeof setTimeout> | null = null
let activePromptPopoverTarget: HTMLElement | null = null
const geminiApiKeys = computed(() =>
apiKeys.value.filter((key) =>
key.status === 'active' &&
key.group?.platform === 'gemini' &&
key.group?.allow_batch_image_generation === true,
),
)
const selectedApiKey = computed(() =>
geminiApiKeys.value.find((key) => key.id === Number(form.apiKeyId)) || null,
)
const filteredApiKeys = computed(() => {
const selectedFilterID = Number(filters.apiKeyId || 0)
if (!selectedFilterID) return geminiApiKeys.value
return geminiApiKeys.value.filter(key => key.id === selectedFilterID)
})
const apiKeyFilterOptions = computed<SelectOption[]>(() => [
{ value: '', label: '全部 API Key' },
...geminiApiKeys.value.map(key => ({
value: String(key.id),
label: key.name || `API Key #${key.id}`,
})),
])
const selectedRows = computed(() =>
batchJobs.value.filter(job => selectedJobIds.value.has(job.id)),
)
const childrenByParent = computed(() => {
const groups = new Map<string, BatchImageJobRow[]>()
for (const job of batchJobs.value) {
if (!job.parent_batch_id) continue
const rows = groups.get(job.parent_batch_id) || []
rows.push(job)
groups.set(job.parent_batch_id, rows)
}
for (const rows of groups.values()) {
rows.sort((a, b) => a.created_at - b.created_at)
}
return groups
})
const visibleBatchJobs = computed(() => {
const rows: BatchImageJobRow[] = []
for (const job of batchJobs.value.filter(item => !item.parent_batch_id)) {
rows.push(job)
if (expandedParentIds.value.has(job.id)) {
rows.push(...(childrenByParent.value.get(job.id) || []).map(child => ({ ...child, is_child: true })))
}
}
return rows
})
const selectedDownloadableRows = computed(() =>
selectedRows.value.filter(job => canDownload(job)),
)
const allVisibleSelected = computed(() =>
visibleBatchJobs.value.length > 0 && visibleBatchJobs.value.every(job => selectedJobIds.value.has(job.id)),
)
const someVisibleSelected = computed(() =>
visibleBatchJobs.value.some(job => selectedJobIds.value.has(job.id)) && !allVisibleSelected.value,
)
const previewImageUrl = computed(() => {
const item = previewImageItem.value
if (!item) return ''
return itemPreviewUrls[itemPreviewKey(item)] || ''
})
const recoveredOriginalCustomIds = computed(() => {
const rootBatchId = detailRootBatchId()
if (!rootBatchId) return new Set<string>()
const ids = new Set<string>()
for (const item of items.value) {
if (!isChildDetailItem(item) || !isSuccessfulImageItem(item)) continue
const sourceCustomID = retrySourceCustomID(item.custom_id)
if (sourceCustomID) ids.add(sourceCustomID)
}
return ids
})
const currentDisplayJob = computed(() => {
if (!currentJob.value) return null
return displayJob(currentJob.value)
})
const endpointBase = computed(() => {
const configured = appStore.apiBaseUrl?.trim()
if (configured) return configured.replace(/\/+$/, '')
if (typeof window !== 'undefined') return window.location.origin.replace(/\/+$/, '')
return '<你的 Sub2API API 端点>'
})
const parsedItems = computed<BatchImageSubmitItem[]>(() => {
const used = new Set<string>()
return promptRows.value
.map((row, index) => {
const customID = uniqueCustomID(row.custom_id || `img_${String(index + 1).padStart(3, '0')}`, used, index)
return { custom_id: customID, prompt: row.prompt.trim() }
})
.filter(item => item.prompt)
})
const agentInstruction = computed(() => `---
name: sub2api-batch-image
description: 当用户希望用 Gemini/Vertex 批量生成图片、批量跑提示词、下载批量生图结果、重试失败图片时使用。
---
你是 Codex 中的批量生图执行 Agent。用户不需要手动填写页面表单;你应从当前聊天、用户给的文件、目录或上下文中整理任务名称、prompt 列表和输出目录,只有缺少关键决策时才向用户提问。
默认端点:
${endpointBase.value}
你需要自己完成:
1. 从用户聊天或附件中提取 prompt。每条 prompt 保留完整文本,按顺序生成稳定 custom_id,例如 img_001、img_002。
2. 从用户要求或上下文推断任务名称;没有明确名称时用当前时间生成任务名。
3. 从用户要求或上下文推断输出目录;如果用户没有说保存到哪里,才询问用户。
4. 选择 API Key 和模型:先获取当前可用的批量生图 Key/模型;如果用户指定模型且该 Key 支持,则使用用户指定模型;否则使用该 Key 可用模型中的默认/第一个。不要展示或询问内部 provider 名称。
5. 调用批量生图 API 提交、轮询、下载,不要求用户去页面里手填。
API 调用规范:
- 模型:GET ${joinEndpointPath(endpointBase.value, '/v1/images/batches/models')}
- 提交:POST ${joinEndpointPath(endpointBase.value, '/v1/images/batches')}
- 查询:GET ${joinEndpointPath(endpointBase.value, '/v1/images/batches/{id}')}
- 明细:GET ${joinEndpointPath(endpointBase.value, '/v1/images/batches/{id}/items')}
- 下载:GET ${joinEndpointPath(endpointBase.value, '/v1/images/batches/{id}/download')}
- 取消:POST ${joinEndpointPath(endpointBase.value, '/v1/images/batches/{id}/cancel')}
提交请求体:
{
"model": "<按所选 Key 可用模型填写>",
"task_name": "<从聊天推断;为空则用当前时间>",
"image_size": "1K",
"response_mime_type": "image/png",
"items": [
{ "custom_id": "img_001", "prompt": "<第一条完整 prompt>" }
]
}
必须遵守:
- 不要把 API Key 写入仓库、日志、提交记录或最终回复。
- 提交成功后,必须立刻在输出目录写入本地恢复记录,例如 batch-image-resume.json。不要在恢复记录里保存 API Key。
- 恢复记录至少包含:endpoint、task_name、batch_id、model、output_dir、request_file、submitted_at、last_status、status_url、items_url、download_url、prompt_count,以及可用于失败重试的 custom_id 到 prompt 映射或请求 JSON 文件路径。
- 每次查询状态后更新恢复记录,写入 last_checked_at、last_status、成功数、失败数、实际扣费和失败摘要。会话中断或暂停后,下次必须能凭该文件继续查询、下载或重试。
- 不要高频轮询。首次查询等待约 20 到 30 秒;queued 状态每 60 到 120 秒查询一次;如果连续 3 次仍是 queued,就先停止主动查询,告诉用户任务仍在排队,并保留恢复记录,之后可继续其他任务或等待用户稍后让你恢复。
- running 状态每约 60 秒查询一次,服务器压力大或大批量任务时可以更久;processing_results 等接近完成的状态可每 20 到 45 秒查询一次。
- 任务完成后报告任务名、任务 id、成功数、失败数、实际扣费和保存路径。
- 只下载成功图片。部分失败时,先展示失败 custom_id、错误码、错误来源和简要原因。
- 重试只能重试失败项,不能重复提交已成功项。若历史任务没有保存失败项 prompt,必须告诉用户无法自动重试,并询问用户是否提供原 prompt。
- 取消任务前必须提醒:已被系统索引为成功的图片仍会按成功项结算扣费,其余冻结金额会释放。
- 图片预览按需加载;不要为了查看列表自动批量加载图片内容。`)
function joinEndpointPath(base: string, path: string): string {
return `${base.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`
}
function uniqueCustomID(raw: string, used: Set<string>, index: number): string {
const base = raw.replace(/[^\w.-]+/g, '_').replace(/^_+|_+$/g, '') || `img_${String(index + 1).padStart(3, '0')}`
let candidate = base
let suffix = 2
while (used.has(candidate)) {
candidate = `${base}_${suffix}`
suffix += 1
}
used.add(candidate)
return candidate
}
function addPromptRow() {
const prompt = promptDraft.value.trim()
if (!prompt) return
const used = new Set(promptRows.value.map(row => row.custom_id))
const customID = uniqueCustomID(customIdDraft.value || `img_${String(promptRows.value.length + 1).padStart(3, '0')}`, used, promptRows.value.length)
promptRows.value = [
...promptRows.value,
{
localId: `${Date.now()}-${Math.random().toString(36).slice(2, 8)}`,
custom_id: customID,
prompt,
},
]
promptDraft.value = ''
customIdDraft.value = ''
}
function removePromptRow(index: number) {
promptRows.value = promptRows.value.filter((_, currentIndex) => currentIndex !== index)
}
async function loadApiKeys() {
loadingKeys.value = true
try {
const response = await keysAPI.list(1, 100, { status: 'active', sort_by: 'created_at', sort_order: 'desc' })
apiKeys.value = response.items || []
if (!selectedApiKey.value && geminiApiKeys.value.length > 0) {
form.apiKeyId = geminiApiKeys.value[0].id
}
if (filters.apiKeyId && !geminiApiKeys.value.some(key => String(key.id) === filters.apiKeyId)) {
filters.apiKeyId = ''
}
if (!selectedApiKey.value) {
availableBatchImageModels.value = []
form.model = ''
}
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('loadKeysFailed')))
} finally {
loadingKeys.value = false
}
}
async function loadAvailableModels() {
const key = selectedApiKey.value
const requestID = ++modelRequestSeq
modelLoadError.value = ''
availableBatchImageModels.value = []
form.model = ''
if (!key) return
loadingModels.value = true
try {
const result = await listBatchImageModels(key.key)
if (requestID !== modelRequestSeq) return
const seen = new Set<string>()
availableBatchImageModels.value = (result.data || [])
.map(model => String(model.id || '').trim())
.filter((model) => {
if (!model || seen.has(model)) return false
seen.add(model)
return true
})
.map(model => ({ value: model, label: model }))
form.model = availableBatchImageModels.value[0]?.value || ''
} catch (error: any) {
if (requestID !== modelRequestSeq) return
modelLoadError.value = batchImageErrorMessage(error, batchImageText('loadModelsFailed'))
} finally {
if (requestID === modelRequestSeq) {
loadingModels.value = false
}
}
}
async function refreshPage() {
await loadApiKeys()
await loadBatchJobs()
}
function applyFilters() {
pagination.page = 1
selectedJobIds.value = new Set()
void loadBatchJobs()
}
function resetFilters() {
filters.taskName = ''
filters.apiKeyId = ''
filters.status = ''
filters.downloaded = ''
applyFilters()
}
function listOptions(): BatchImageJobsListOptions {
const options: BatchImageJobsListOptions = {
limit: pagination.page_size,
cursor: String((pagination.page - 1) * pagination.page_size),
}
if (filters.taskName.trim()) options.taskName = filters.taskName.trim()
if (filters.status) options.status = filters.status
if (filters.downloaded) options.downloaded = filters.downloaded
return options
}
function toJobRow(job: BatchImageJob, key = selectedApiKey.value): BatchImageJobRow {
return {
id: job.id,
task_name: job.task_name || defaultTaskName(job.created_at),
parent_batch_id: job.parent_batch_id || null,
status: job.status,
model: job.model,
provider: job.provider,
item_count: job.item_count,
success_count: job.success_count,
fail_count: job.fail_count,
estimated_cost: job.estimated_cost,
hold_amount: job.hold_amount,
actual_cost: job.actual_cost,
created_at: job.created_at,
downloaded_at: job.downloaded_at,
api_key_id: key?.id || 0,
api_key_name: key?.name || '',
child_count: 0,
}
}
function applyChildCounts(rows: BatchImageJobRow[]) {
const counts = new Map<string, number>()
for (const row of rows) {
if (!row.parent_batch_id) continue
counts.set(row.parent_batch_id, (counts.get(row.parent_batch_id) || 0) + 1)
}
return rows.map(row => ({ ...row, child_count: counts.get(row.id) || 0 }))
}
function displayJob<T extends Pick<BatchImageJob, 'id' | 'parent_batch_id' | 'status' | 'item_count' | 'success_count' | 'fail_count' | 'estimated_cost' | 'hold_amount' | 'actual_cost'>>(job: T): T {
if (job.parent_batch_id) return job
const children = childrenByParent.value.get(job.id) || []
if (!children.length) return job
const childSuccess = children.reduce((sum, child) => sum + child.success_count, 0)
const childEstimated = children.reduce((sum, child) => sum + child.estimated_cost, 0)
const childHold = children.reduce((sum, child) => sum + child.hold_amount, 0)
const childActual = children.reduce((sum, child) => sum + (child.actual_cost || 0), 0)
const childActualReady = children.every(child => child.actual_cost !== null)
const successCount = Math.min(job.item_count, job.success_count + childSuccess)
const failCount = Math.max(0, job.item_count - successCount)
const actualCost = job.actual_cost === null
? (childActualReady ? childActual : null)
: job.actual_cost + childActual
return {
...job,
success_count: successCount,
fail_count: failCount,
status: failCount === 0 && TERMINAL_STATUSES.has(job.status) ? 'completed' : job.status,
estimated_cost: job.estimated_cost + childEstimated,
hold_amount: job.hold_amount + childHold,
actual_cost: actualCost,
}
}
function hasChildJobs(batchId: string) {
return (childrenByParent.value.get(batchId) || []).length > 0
}
function toggleChildRows(batchId: string) {
const next = new Set(expandedParentIds.value)
if (next.has(batchId)) next.delete(batchId)
else next.add(batchId)
expandedParentIds.value = next
}
function closeMoreMenu() {
openMoreJobId.value = ''
}
function toggleMoreMenu(job: BatchImageJobRow, event: MouseEvent) {
if (openMoreJobId.value === job.id) {
closeMoreMenu()
return
}
const trigger = event.currentTarget as HTMLElement | null
const rect = trigger?.getBoundingClientRect()
if (!rect) return
const menuWidth = 176
const margin = 8
const left = Math.max(margin, Math.min(rect.right - menuWidth, window.innerWidth - menuWidth - margin))
const top = Math.min(rect.bottom + margin, window.innerHeight - 96)
moreMenuStyle.value = {
left: `${left}px`,
top: `${Math.max(margin, top)}px`,
}
openMoreJobId.value = job.id
}
function cancelPromptPopoverClose() {
if (!promptPopoverCloseTimer) return
clearTimeout(promptPopoverCloseTimer)
promptPopoverCloseTimer = null
}
function cancelPromptPopoverOpen() {
if (!promptPopoverOpenTimer) return
clearTimeout(promptPopoverOpenTimer)
promptPopoverOpenTimer = null
}
function closePromptPopover() {
cancelPromptPopoverOpen()
cancelPromptPopoverClose()
promptPopover.visible = false
promptPopover.text = ''
promptPopover.style = {}
activePromptPopoverTarget = null
}
function schedulePromptPopoverClose() {
cancelPromptPopoverOpen()
cancelPromptPopoverClose()
promptPopoverCloseTimer = setTimeout(() => {
closePromptPopover()
}, 180)
}
function schedulePromptPopoverOpen(event: MouseEvent | PointerEvent, text: string) {
const target = event.currentTarget as HTMLElement | null
if (!target) return
const value = String(text || '').trim()
if (!value || value === '-') return
activePromptPopoverTarget = target
cancelPromptPopoverOpen()
cancelPromptPopoverClose()
promptPopoverOpenTimer = setTimeout(() => {
if (activePromptPopoverTarget !== target || !document.body.contains(target)) return
openPromptPopover(target, value)
}, 520)
}
function showPromptPopover(event: MouseEvent | FocusEvent, text: string) {
const value = String(text || '').trim()
if (!value || value === '-') return
const target = event.currentTarget as HTMLElement | null
cancelPromptPopoverClose()
cancelPromptPopoverOpen()
if (!target) return
activePromptPopoverTarget = target
openPromptPopover(target, value)
}
function openPromptPopover(target: HTMLElement, value: string) {
const rect = target.getBoundingClientRect()
if (!rect) return
const viewportWidth = window.innerWidth || 1280
const viewportHeight = window.innerHeight || 720
const width = Math.min(440, Math.max(320, viewportWidth - 32))
const left = Math.max(16, Math.min(rect.left, viewportWidth - width - 16))
const estimatedHeight = 178
const preferredTop = rect.bottom + 8
const top = preferredTop + estimatedHeight > viewportHeight
? Math.max(16, rect.top - estimatedHeight - 8)
: preferredTop
promptPopover.text = value
promptPopover.style = {
left: `${left}px`,
top: `${top}px`,
width: `${width}px`,
}
promptPopover.visible = true
}
function copyPromptPopover() {
if (!promptPopover.text) return
void copyToClipboard(promptPopover.text, 'Prompt 已复制')
}
async function loadBatchJobs() {
const keys = filteredApiKeys.value
if (!keys.length) {
batchJobs.value = []
pagination.has_more = false
return
}
loadingJobs.value = true
closeMoreMenu()
try {
const options = listOptions()
const results = await Promise.all(keys.map(async (key) => {
const result = await listBatchImageJobs(key.key, options)
return {
hasMore: Boolean(result.has_more),
rows: (result.data || []).map(job => toJobRow(job, key)),
}
}))
batchJobs.value = applyChildCounts(results
.flatMap(result => result.rows)
.sort((a, b) => b.created_at - a.created_at)
.slice(0, pagination.page_size))
pagination.has_more = results.some(result => result.hasMore)
selectedJobIds.value = new Set([...selectedJobIds.value].filter(id => visibleBatchJobs.value.some(job => job.id === id)))
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('loadJobsFailed')))
} finally {
loadingJobs.value = false
}
}
function upsertJob(job: BatchImageJob) {
const next = toJobRow(job)
const index = batchJobs.value.findIndex(item => item.id === job.id)
if (index >= 0) {
const rows = [...batchJobs.value]
rows[index] = { ...next, is_child: rows[index].is_child }
batchJobs.value = applyChildCounts(rows)
return
}
batchJobs.value = applyChildCounts([next, ...batchJobs.value].slice(0, pagination.page_size))
}
function handlePageChange(page: number) {
if (page < 1 || page === pagination.page) return
pagination.page = page
selectedJobIds.value = new Set()
void loadBatchJobs()
}
function handlePageSizeChange(value: string | number | boolean | null) {
if (value === null || typeof value === 'boolean') return
const nextSize = Math.min(Math.max(Number(value) || 20, 1), 100)
pagination.page_size = nextSize
pagination.page = 1
setPersistedPageSize(nextSize)
selectedJobIds.value = new Set()
void loadBatchJobs()
}
function openCreateModal() {
showCreateModal.value = true
if (!apiKeys.value.length) {
void loadApiKeys()
}
}
function closeCreateModal() {
showCreateModal.value = false
}
function resetCreateDraft() {
form.taskName = ''
promptRows.value = []
promptDraft.value = ''
customIdDraft.value = ''
}
function closeDetail() {
closePromptPopover()
currentJob.value = null
selectedBatchId.value = ''
selectedBatchApiKeyId.value = 0
items.value = []
clearItemPreviews()
}
function keyForSelectedBatch(): ApiKey | null {
if (selectedBatchApiKeyId.value) {
const key = geminiApiKeys.value.find(item => item.id === selectedBatchApiKeyId.value)
if (key) return key
}
return selectedApiKey.value
}
function requireApiKey(): ApiKey | null {
if (!selectedApiKey.value) {
appStore.showError(batchImageText('selectApiKey'))
return null
}
return selectedApiKey.value
}
function validateForm(): boolean {
if (!requireApiKey()) return false
if (!form.model) {
appStore.showError(availableBatchImageModels.value.length === 0 ? batchImageText('noModelsForKey') : batchImageText('selectModel'))
return false
}
if (parsedItems.value.length === 0) {
appStore.showError(batchImageText('promptRequired'))
return false
}
return true
}
async function submitJob() {
if (submitting.value) return
if (promptDraft.value.trim()) addPromptRow()
if (!validateForm()) return
const key = requireApiKey()
if (!key) return
submitting.value = true
try {
const job = await submitBatchImageJob(
key.key,
{
model: form.model,
task_name: form.taskName.trim() || defaultTaskName(),
image_size: '1K',
response_mime_type: form.responseMimeType,
items: parsedItems.value,
},
`sub2api-ui-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`,
)
currentJob.value = job
selectedBatchId.value = job.id
selectedBatchApiKeyId.value = key.id
items.value = []
upsertJob(job)
showCreateModal.value = false
resetCreateDraft()
appStore.showSuccess(batchImageText('submitted'))
void loadItems()
startPolling()
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('submitFailed')))
} finally {
submitting.value = false
}
}
async function refreshSelected() {
if (!selectedBatchId.value) return
const key = keyForSelectedBatch() || requireApiKey()
if (!key) return
refreshing.value = true
try {
const job = await getBatchImageJob(key.key, selectedBatchId.value)
currentJob.value = job
upsertJob(job)
if (TERMINAL_STATUSES.has(job.status)) stopPolling()
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('refreshFailed')))
} finally {
refreshing.value = false
}
}
async function refreshDetail() {
await Promise.all([
refreshSelected(),
loadItems(),
])
}
function selectJob(batchId: string) {
const row = batchJobs.value.find(job => job.id === batchId)
if (row?.api_key_id && geminiApiKeys.value.some(key => key.id === row.api_key_id)) {
form.apiKeyId = row.api_key_id
selectedBatchApiKeyId.value = row.api_key_id
} else {
selectedBatchApiKeyId.value = 0
}
selectedBatchId.value = batchId
currentJob.value = null
items.value = []
void refreshSelected()
void loadItems()
}
function startPolling() {
stopPolling()
pollTimer = setInterval(() => {
if (!currentJob.value || TERMINAL_STATUSES.has(currentJob.value.status)) {
stopPolling()
return
}
void refreshSelected()
}, 8000)
}
function stopPolling() {
if (pollTimer) {
clearInterval(pollTimer)
pollTimer = null
}
}
function canCancel(job: Pick<BatchImageJob, 'status'>) {
return !TERMINAL_STATUSES.has(job.status)
}
function canDownload(job: Pick<BatchImageJob, 'status' | 'success_count'>) {
return job.status === 'completed' && job.success_count > 0
}
function canRetry(job: Pick<BatchImageJob, 'status' | 'fail_count'>) {
const display = 'id' in job ? displayJob(job as BatchImageJob) : job
return TERMINAL_STATUSES.has(display.status) && display.fail_count > 0
}
function isDownloadingJob(batchId: string) {
return downloading.value && downloadingBatchId.value === batchId
}
function applyJobApiKey(job: BatchImageJobRow | Pick<BatchImageJob, 'id'>) {
if ('api_key_id' in job && job.api_key_id && geminiApiKeys.value.some(key => key.id === job.api_key_id)) {
form.apiKeyId = job.api_key_id
}
}
function apiKeyForJob(job: BatchImageJobRow | Pick<BatchImageJob, 'id'>): ApiKey | null {
if ('api_key_id' in job && job.api_key_id) {
return geminiApiKeys.value.find(key => key.id === job.api_key_id) || null
}
return selectedApiKey.value
}
function toggleJobSelection(batchId: string, checked: boolean) {
const next = new Set(selectedJobIds.value)
if (checked) next.add(batchId)
else next.delete(batchId)
selectedJobIds.value = next
}
function toggleAllVisible(checked: boolean) {
const next = new Set(selectedJobIds.value)
for (const job of visibleBatchJobs.value) {
if (checked) next.add(job.id)
else next.delete(job.id)
}
selectedJobIds.value = next
}
function canDeleteRecord(job: Pick<BatchImageJob, 'status'>) {
return TERMINAL_STATUSES.has(job.status)
}
async function cancelSelected() {
if (!currentJob.value) return
const key = keyForSelectedBatch() || requireApiKey()
if (!key) return
if (!window.confirm(batchImageText('cancelConfirm'))) return
cancelling.value = true
try {
const job = await cancelBatchImageJob(key.key, currentJob.value.id)
currentJob.value = job
upsertJob(job)
appStore.showSuccess(batchImageText('cancelled'))
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('cancelFailed')))
} finally {
cancelling.value = false
}
}
async function downloadSelected() {
if (!currentJob.value) return
await downloadJob(currentJob.value)
}
async function retrySelected() {
if (!currentJob.value) return
await retryFailedJob(currentJob.value)
}
async function retryFailedJob(job: BatchImageJobRow | BatchImageJob) {
if (!canRetry(job) || retryingBatchId.value) return
closeMoreMenu()
const key = apiKeyForJob(job) || keyForSelectedBatch() || requireApiKey()
if (!key) return
retryingBatchId.value = job.id
try {
const sourceItems = await ensureItemsForRetry(key.key, job.id)
const failedItems = sourceItems
.filter(item => item.status === 'failed')
.map(item => ({ custom_id: retryCustomID(item.custom_id), prompt: String(item.prompt_preview || '').trim() }))
.filter(item => item.prompt)
if (failedItems.length === 0) {
appStore.showError(batchImageText('retryMissingPrompts'))
return
}
const retryJob = await submitBatchImageJob(
key.key,
{
model: job.model,
task_name: `${job.task_name || defaultTaskName()} 重试失败项`,
parent_batch_id: rootBatchIdForRetry(job),
provider: job.provider,
image_size: '1K',
response_mime_type: form.responseMimeType,
items: failedItems,
},
`sub2api-ui-retry-${job.id}-${Date.now()}-${Math.random().toString(36).slice(2, 10)}`,
)
currentJob.value = retryJob
selectedBatchId.value = retryJob.id
selectedBatchApiKeyId.value = key.id
items.value = []
upsertJob(retryJob)
if (retryJob.parent_batch_id) {
expandedParentIds.value = new Set([...expandedParentIds.value, retryJob.parent_batch_id])
}
appStore.showSuccess(batchImageText('retrySubmitted'))
void loadItems()
startPolling()
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('retryFailed')))
} finally {
retryingBatchId.value = ''
}
}
async function ensureItemsForRetry(apiKey: string, batchId: string) {
if (selectedBatchId.value === batchId && items.value.length > 0) {
return items.value
}
const result = await listBatchImageItems(apiKey, batchId)
return result.data || []
}
function retryCustomID(customID: string) {
const base = String(customID || 'item').replace(/[^\w.-]+/g, '_').replace(/^_+|_+$/g, '') || 'item'
return `${base}_retry_${Date.now().toString(36)}`
}
function rootBatchIdForRetry(job: BatchImageJobRow | BatchImageJob) {
return job.parent_batch_id || job.id
}
async function downloadJob(job: (BatchImageJobRow | Pick<BatchImageJob, 'id'>)) {
if (downloading.value) return
closeMoreMenu()
applyJobApiKey(job)
const key = apiKeyForJob(job) || requireApiKey()
if (!key) return
downloading.value = true
downloadingBatchId.value = job.id
try {
const blob = await downloadBatchImageZip(key.key, job.id)
saveBlob(blob, `${job.id}.zip`)
markJobDownloaded(job.id)
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('downloadFailed')))
} finally {
downloading.value = false
downloadingBatchId.value = ''
}
}
async function downloadSelectedJobs() {
if (bulkDownloading.value || selectedDownloadableRows.value.length === 0) return
bulkDownloading.value = true
try {
for (const row of selectedDownloadableRows.value) {
const key = apiKeyForJob(row)
if (!key) continue
downloading.value = true
downloadingBatchId.value = row.id
const blob = await downloadBatchImageZip(key.key, row.id)
saveBlob(blob, `${row.id}.zip`)
markJobDownloaded(row.id)
}
appStore.showSuccess(batchImageText('batchDownloadStarted'))
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('downloadFailed')))
} finally {
bulkDownloading.value = false
downloading.value = false
downloadingBatchId.value = ''
}
}
async function deleteJob(job: BatchImageJobRow) {
if (!canDeleteRecord(job) || deletingBatchId.value) return
closeMoreMenu()
const key = apiKeyForJob(job)
if (!key) return
if (!window.confirm(batchImageText('deleteConfirm'))) return
deletingBatchId.value = job.id
try {
await deleteBatchImageJobRecord(key.key, job.id)
removeJobFromList(job.id)
appStore.showSuccess(batchImageText('deleted'))
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('deleteFailed')))
} finally {
deletingBatchId.value = ''
}
}
async function deleteSelectedJobs() {
const rows = selectedRows.value.filter(job => canDeleteRecord(job))
if (bulkDeleting.value || rows.length === 0) return
if (!window.confirm(batchImageText('deleteSelectedConfirm'))) return
bulkDeleting.value = true
try {
for (const row of rows) {
const key = apiKeyForJob(row)
if (!key) continue
deletingBatchId.value = row.id
await deleteBatchImageJobRecord(key.key, row.id)
removeJobFromList(row.id)
}
appStore.showSuccess(batchImageText('deleted'))
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('deleteFailed')))
} finally {
bulkDeleting.value = false
deletingBatchId.value = ''
}
}
function markJobDownloaded(batchId: string) {
const downloadedAt = Math.floor(Date.now() / 1000)
batchJobs.value = batchJobs.value.map(job => job.id === batchId ? { ...job, downloaded_at: job.downloaded_at || downloadedAt } : job)
if (currentJob.value?.id === batchId && !currentJob.value.downloaded_at) {
currentJob.value = { ...currentJob.value, downloaded_at: downloadedAt }
}
}
function removeJobFromList(batchId: string) {
batchJobs.value = batchJobs.value.filter(job => job.id !== batchId)
toggleJobSelection(batchId, false)
if (currentJob.value?.id === batchId) closeDetail()
}
function canLoadItemPreview(item: BatchImageItem) {
return (item.status === 'succeeded' || item.status === 'success') && item.image_count > 0
}
function isSuccessfulImageItem(item: Pick<BatchImageItem, 'status' | 'image_count'>) {
return (item.status === 'succeeded' || item.status === 'success') && item.image_count > 0
}
function detailRootBatchId() {
return currentJob.value?.parent_batch_id || selectedBatchId.value || currentJob.value?.id || ''
}
function isChildDetailItem(item: Pick<BatchImageDetailItem, 'batch_id'>) {
const rootBatchId = detailRootBatchId()
return Boolean(rootBatchId && item.batch_id && item.batch_id !== rootBatchId)
}
function retrySourceCustomID(customID: string) {
return String(customID || '').replace(/(?:_retry_[a-z0-9]+)+$/i, '')
}
function isRecoveredOriginalFailure(item: BatchImageDetailItem) {
const rootBatchId = detailRootBatchId()
return Boolean(
rootBatchId
&& item.batch_id === rootBatchId
&& item.status === 'failed'
&& recoveredOriginalCustomIds.value.has(item.custom_id),
)
}
function detailItemRowClass(item: BatchImageDetailItem) {
if (isRecoveredOriginalFailure(item)) {
return 'bg-gray-50/80 text-gray-400 hover:bg-gray-100/80 dark:bg-dark-900/60 dark:text-gray-500 dark:hover:bg-dark-800/70'
}
return 'hover:bg-gray-50/70 dark:hover:bg-dark-800/60'
}
function previewCacheSupported() {
return typeof window !== 'undefined' && 'indexedDB' in window
}
function previewCacheKey(batchId: string, customID: string, imageIndex = 0) {
return [batchId, customID, imageIndex].map(part => encodeURIComponent(String(part))).join(':')
}
function itemPreviewKey(item: Pick<BatchImageItem, 'batch_id' | 'custom_id'>) {
return previewCacheKey(item.batch_id || selectedBatchId.value || currentJob.value?.id || '', item.custom_id, 0)
}
function idbRequest<T>(request: IDBRequest<T>): Promise<T> {
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(request.result)
request.onerror = () => reject(request.error)
})
}
function openPreviewCacheDB(): Promise<IDBDatabase | null> {
if (!previewCacheSupported()) return Promise.resolve(null)
if (previewCacheDBPromise) return previewCacheDBPromise
previewCacheDBPromise = new Promise((resolve) => {
const request = window.indexedDB.open(PREVIEW_CACHE_DB_NAME, 1)
request.onupgradeneeded = () => {
const db = request.result
if (!db.objectStoreNames.contains(PREVIEW_CACHE_STORE_NAME)) {
const store = db.createObjectStore(PREVIEW_CACHE_STORE_NAME, { keyPath: 'key' })
store.createIndex('lastAccessedAt', 'lastAccessedAt', { unique: false })
}
}
request.onsuccess = () => resolve(request.result)
request.onerror = () => resolve(null)
request.onblocked = () => resolve(null)
})
return previewCacheDBPromise
}
async function getCachedPreviewBlob(cacheKey: string): Promise<Blob | null> {
const db = await openPreviewCacheDB()
if (!db) return null
const record = await idbRequest<PreviewCacheRecord | undefined>(
db.transaction(PREVIEW_CACHE_STORE_NAME, 'readonly').objectStore(PREVIEW_CACHE_STORE_NAME).get(cacheKey),
).catch(() => undefined)
if (!record?.blob) return null
const now = Date.now()
if (now - record.createdAt > PREVIEW_CACHE_MAX_AGE_MS) {
void deleteCachedPreview(cacheKey)
return null
}
void touchCachedPreview(cacheKey, now)
return record.blob
}
async function hydrateCachedItemPreviews(detailItems: BatchImageDetailItem[]) {
const previewableItems = detailItems.filter(item => canLoadItemPreview(item))
if (!previewableItems.length || !previewCacheSupported()) return
await Promise.all(previewableItems.map(async (item) => {
const batchId = item.batch_id || selectedBatchId.value || currentJob.value?.id || ''
const previewKey = itemPreviewKey(item)
if (!batchId || itemPreviewUrls[previewKey] || previewErrorIds.value.has(previewKey)) return
const cached = await getCachedPreviewBlob(previewCacheKey(batchId, item.custom_id, 0)).catch(() => null)
if (!cached || itemPreviewUrls[previewKey]) return
itemPreviewUrls[previewKey] = URL.createObjectURL(cached)
}))
}
async function putCachedPreviewBlob(cacheKey: string, blob: Blob) {
const db = await openPreviewCacheDB()
if (!db) return
const now = Date.now()
const record: PreviewCacheRecord = {
key: cacheKey,
blob,
size: blob.size,
createdAt: now,
lastAccessedAt: now,
}
await idbRequest(db.transaction(PREVIEW_CACHE_STORE_NAME, 'readwrite').objectStore(PREVIEW_CACHE_STORE_NAME).put(record)).catch(() => null)
void cleanupPreviewCache()
}
async function touchCachedPreview(cacheKey: string, lastAccessedAt: number) {
const db = await openPreviewCacheDB()
if (!db) return
const record = await idbRequest<PreviewCacheRecord | undefined>(
db.transaction(PREVIEW_CACHE_STORE_NAME, 'readonly').objectStore(PREVIEW_CACHE_STORE_NAME).get(cacheKey),
).catch(() => undefined)
if (!record) return
record.lastAccessedAt = lastAccessedAt
await idbRequest(db.transaction(PREVIEW_CACHE_STORE_NAME, 'readwrite').objectStore(PREVIEW_CACHE_STORE_NAME).put(record)).catch(() => null)
}
async function deleteCachedPreview(cacheKey: string) {
const db = await openPreviewCacheDB()
if (!db) return
await idbRequest(db.transaction(PREVIEW_CACHE_STORE_NAME, 'readwrite').objectStore(PREVIEW_CACHE_STORE_NAME).delete(cacheKey)).catch(() => null)
}
async function cleanupPreviewCache() {
const db = await openPreviewCacheDB()
if (!db) return
const records = await idbRequest<PreviewCacheRecord[]>(
db.transaction(PREVIEW_CACHE_STORE_NAME, 'readonly').objectStore(PREVIEW_CACHE_STORE_NAME).getAll(),
).catch(() => [])
if (!records.length) return
const now = Date.now()
const sorted = [...records].sort((a, b) => a.lastAccessedAt - b.lastAccessedAt)
const deleteKeys = new Set<string>()
let totalBytes = 0
let keptCount = 0
for (const record of sorted) {
if (now - record.createdAt > PREVIEW_CACHE_MAX_AGE_MS) {
deleteKeys.add(record.key)
continue
}
totalBytes += record.size || record.blob?.size || 0
keptCount += 1
}
for (const record of sorted) {
if (deleteKeys.has(record.key)) continue
if (keptCount <= PREVIEW_CACHE_MAX_ENTRIES && totalBytes <= PREVIEW_CACHE_MAX_BYTES) break
deleteKeys.add(record.key)
totalBytes -= record.size || record.blob?.size || 0
keptCount -= 1
}
if (!deleteKeys.size) return
const store = db.transaction(PREVIEW_CACHE_STORE_NAME, 'readwrite').objectStore(PREVIEW_CACHE_STORE_NAME)
for (const key of deleteKeys) {
store.delete(key)
}
}
async function createThumbnailBlob(blob: Blob): Promise<Blob> {
const source = await loadPreviewImageSource(blob)
const width = source.width
const height = source.height
const scale = Math.min(1, PREVIEW_THUMBNAIL_MAX_EDGE / Math.max(width, height))
const targetWidth = Math.max(1, Math.round(width * scale))
const targetHeight = Math.max(1, Math.round(height * scale))
const canvas = document.createElement('canvas')
canvas.width = targetWidth
canvas.height = targetHeight
const ctx = canvas.getContext('2d')
if (!ctx) throw new Error('canvas unavailable')
ctx.drawImage(source.image, 0, 0, targetWidth, targetHeight)
source.close()
return await new Promise<Blob>((resolve, reject) => {
canvas.toBlob((thumbnail) => {
if (thumbnail) resolve(thumbnail)
else reject(new Error('thumbnail unavailable'))
}, 'image/webp', PREVIEW_THUMBNAIL_QUALITY)
})
}
async function loadPreviewImageSource(blob: Blob): Promise<{ image: PreviewImageSource, width: number, height: number, close: () => void }> {
if ('createImageBitmap' in window) {
const bitmap = await window.createImageBitmap(blob)
return {
image: bitmap,
width: bitmap.width,
height: bitmap.height,
close: () => bitmap.close(),
}
}
const url = URL.createObjectURL(blob)
try {
const image = await new Promise<HTMLImageElement>((resolve, reject) => {
const img = new Image()
img.onload = () => resolve(img)
img.onerror = () => reject(new Error('image unavailable'))
img.src = url
})
return {
image,
width: image.naturalWidth || image.width,
height: image.naturalHeight || image.height,
close: () => URL.revokeObjectURL(url),
}
} catch (error) {
URL.revokeObjectURL(url)
throw error
}
}
async function loadItems() {
const batchId = selectedBatchId.value || currentJob.value?.id || ''
if (!batchId) return
const key = keyForSelectedBatch() || requireApiKey()
if (!key) return
loadingItems.value = true
try {
clearItemPreviews()
const jobs = detailJobsForBatch(batchId)
const results = await Promise.all(jobs.map(async (job) => {
const result = await listBatchImageItems(key.key, job.id)
return (result.data || []).map(item => ({
...item,
batch_id: job.id,
source_task_name: detailSourceName(job, batchId),
}))
}))
const detailItems = results.flat()
items.value = detailItems
void hydrateCachedItemPreviews(detailItems)
} catch (error: any) {
appStore.showError(batchImageErrorMessage(error, batchImageText('loadItemsFailed')))
} finally {
loadingItems.value = false
}
}
function detailJobsForBatch(batchId: string): BatchImageJobRow[] {
const row = batchJobs.value.find(job => job.id === batchId)
const base = row || (currentJob.value && currentJob.value.id === batchId ? toJobRow(currentJob.value, keyForSelectedBatch() || selectedApiKey.value) : null)
if (!base) return []
if (base.parent_batch_id) return [base]
return [base, ...(childrenByParent.value.get(base.id) || [])]
}
function detailSourceName(job: Pick<BatchImageJobRow, 'id' | 'task_name' | 'parent_batch_id'>, rootBatchId: string) {
const name = job.task_name || job.id
if (job.id === rootBatchId) return `主任务:${name}`
return `子任务:${name}`
}
async function loadItemPreview(item: BatchImageItem) {
const batchId = item.batch_id || selectedBatchId.value || currentJob.value?.id || ''
const previewKey = itemPreviewKey(item)
if (!batchId || !canLoadItemPreview(item) || (itemPreviewUrls[previewKey] && !previewErrorIds.value.has(previewKey))) return
const key = keyForSelectedBatch() || requireApiKey()
if (!key) return
const cacheKey = previewCacheKey(batchId, item.custom_id, 0)
previewLoadingIds.value = new Set([...previewLoadingIds.value, previewKey])
try {
previewErrorIds.value = new Set([...previewErrorIds.value].filter(id => id !== previewKey))
if (itemPreviewUrls[previewKey]) {
URL.revokeObjectURL(itemPreviewUrls[previewKey])
delete itemPreviewUrls[previewKey]
}
const cached = await getCachedPreviewBlob(cacheKey)
if (cached) {
itemPreviewUrls[previewKey] = URL.createObjectURL(cached)
return
}
const blob = await getBatchImageItemContent(key.key, batchId, item.custom_id, 0)
const thumbnail = await createThumbnailBlob(blob).catch(() => blob)
itemPreviewUrls[previewKey] = URL.createObjectURL(thumbnail)
if (thumbnail !== blob || thumbnail.size <= 1024 * 1024) {
void putCachedPreviewBlob(cacheKey, thumbnail)
}
} catch (error: any) {
previewErrorIds.value = new Set([...previewErrorIds.value, previewKey])
appStore.showError(batchImageErrorMessage(error, batchImageText('loadPreviewFailed')))
} finally {
const next = new Set(previewLoadingIds.value)
next.delete(previewKey)
previewLoadingIds.value = next
}
}
function openImagePreview(item: BatchImageItem) {
const previewKey = itemPreviewKey(item)
if (!itemPreviewUrls[previewKey] || previewErrorIds.value.has(previewKey)) return
previewImageItem.value = item
}
function closeImagePreview() {
previewImageItem.value = null
}
function handlePreviewError(customID: string) {
if (itemPreviewUrls[customID]) {
URL.revokeObjectURL(itemPreviewUrls[customID])
delete itemPreviewUrls[customID]
}
previewErrorIds.value = new Set([...previewErrorIds.value, customID])
}
function clearItemPreviews() {
closePromptPopover()
for (const url of Object.values(itemPreviewUrls)) {
if (url) URL.revokeObjectURL(url)
}
for (const key of Object.keys(itemPreviewUrls)) {
delete itemPreviewUrls[key]
}
previewLoadingIds.value = new Set()
previewErrorIds.value = new Set()
previewImageItem.value = null
}
function copyInstruction() {
void copyToClipboard(agentInstruction.value, batchImageText('copiedInstruction'))
}
function statusLabel(jobOrStatus: BatchImageStatus | Pick<BatchImageJob, 'status' | 'success_count' | 'fail_count'>) {
const status = typeof jobOrStatus === 'string' ? jobOrStatus : jobOrStatus.status
if (typeof jobOrStatus !== 'string' && status === 'completed' && jobOrStatus.fail_count > 0) {
if (jobOrStatus.success_count > 0) return '部分成功'
return '全部失败'
}
const labels: Record<string, string> = {
queued: '排队中',
running: '生成中',
indexing: '整理结果',
processing_results: '整理结果',
settling: '结算中',
completed: '已完成',
failed: '失败',
cancelled: '已取消',
output_deleted: '结果已删除',
}
return labels[status] || status
}
function statusBadgeClass(jobOrStatus: BatchImageStatus | Pick<BatchImageJob, 'status' | 'success_count' | 'fail_count'>) {
const status = typeof jobOrStatus === 'string' ? jobOrStatus : jobOrStatus.status
if (typeof jobOrStatus !== 'string' && status === 'completed' && jobOrStatus.fail_count > 0) {
if (jobOrStatus.success_count > 0) return 'badge-warning'
return 'badge-danger'
}
if (status === 'completed') return 'badge-success'
if (status === 'failed' || status === 'cancelled') return 'badge-danger'
if (status === 'output_deleted') return 'badge-gray'
return 'badge-primary'
}
function itemStatusLabel(status: string) {
const labels: Record<string, string> = {
pending: '排队中',
succeeded: '成功',
success: '成功',
failed: '失败',
cancelled: '已取消',
}
return labels[status] || status
}
function itemDisplayStatusLabel(item: BatchImageDetailItem) {
if (isRecoveredOriginalFailure(item)) return '已补成功'
return itemStatusLabel(item.status)
}
function itemStatusBadgeClass(status: string) {
if (status === 'succeeded' || status === 'success') return 'badge-success'
if (status === 'failed' || status === 'cancelled') return 'badge-danger'
return 'badge-primary'
}
function itemDisplayStatusBadgeClass(item: BatchImageDetailItem) {
if (isRecoveredOriginalFailure(item)) return 'badge-gray'
return itemStatusBadgeClass(item.status)
}
function itemResultLabel(item: BatchImageDetailItem) {
if (isRecoveredOriginalFailure(item)) return '旧失败已由重试子任务补成功'
if (item.error) return friendlyItemError(item.error)
if (item.status === 'succeeded' || item.status === 'success') {
return itemPreviewUrls[itemPreviewKey(item)] ? '图片已生成,可点击预览' : '图片已生成,可下载'
}
if (item.status === 'failed') return '未生成可用图片'
if (item.status === 'cancelled') return '任务已取消'
return '等待生成结果'
}
function itemResultClass(item: BatchImageDetailItem) {
if (isRecoveredOriginalFailure(item)) return 'bg-gray-100 text-gray-500 ring-gray-200 dark:bg-dark-800 dark:text-gray-400 dark:ring-dark-700'
if (item.error || item.status === 'failed' || item.status === 'cancelled') return 'bg-red-50 text-red-700 ring-red-100 dark:bg-red-950/30 dark:text-red-300 dark:ring-red-900/50'
if (item.status === 'succeeded' || item.status === 'success') return 'bg-emerald-50 text-emerald-700 ring-emerald-100 dark:bg-emerald-950/30 dark:text-emerald-300 dark:ring-emerald-900/50'
return 'bg-gray-50 text-gray-500 ring-gray-200 dark:bg-dark-800 dark:text-gray-400 dark:ring-dark-700'
}
function friendlyItemError(error: BatchImageItem['error']) {
if (!error) return '-'
if (error.code === 'EMPTY_IMAGE_OUTPUT') return '上游返回了结果,但这条没有图片内容。通常是 Gemini/Vertex 单条生成失败或被安全策略拦截。'
if (error.code === 'PROVIDER_ITEM_FAILED') return '上游返回的这条结果没有可用图片。'
return error.message || error.code || '-'
}
function formatMoney(value: number | null | undefined) {
if (value === null || value === undefined || Number.isNaN(Number(value))) return '$0.00'
return `$${Number(value).toFixed(2)}`
}
function terminalZeroCost(job: Pick<BatchImageJob, 'status' | 'actual_cost'>) {
return job.actual_cost === null && (job.status === 'failed' || job.status === 'cancelled')
}
function costLabel(job: Pick<BatchImageJob, 'status' | 'hold_amount' | 'actual_cost'>) {
if (job.actual_cost !== null) return formatMoney(job.actual_cost)
if (terminalZeroCost(job)) return formatMoney(0)
return `冻结 ${formatMoney(job.hold_amount)}`
}
type BatchImageTextKey =
| 'loadKeysFailed'
| 'loadModelsFailed'
| 'loadJobsFailed'
| 'selectApiKey'
| 'noModelsForKey'
| 'selectModel'
| 'promptRequired'
| 'submitted'
| 'submitFailed'
| 'refreshFailed'
| 'cancelConfirm'
| 'cancelled'
| 'cancelFailed'
| 'batchDownloadStarted'
| 'downloadFailed'
| 'retrySubmitted'
| 'retryFailed'
| 'retryMissingPrompts'
| 'deleteConfirm'
| 'deleteSelectedConfirm'
| 'deleted'
| 'deleteFailed'
| 'loadItemsFailed'
| 'loadPreviewFailed'
| 'copiedInstruction'
| 'loadingModels'
| 'noModels'
| 'noModelsHint'
| 'noCompatibleAccount'
| 'unsupportedProvider'
| 'providerSubmitFailed'
| 'vertexGcsBucketMissing'
| 'queueFailed'
| 'billingHoldFailed'
| 'groupDisabled'
| 'pricingMissing'
| 'insufficientBalance'
| 'invalidModel'
| 'invalidItems'
| 'duplicateCustomId'
| 'promptTooLong'
| 'idempotencyConflict'
| 'notReady'
| 'outputDeleted'
| 'resultMissing'
| 'itemFailed'
| 'itemImageIndexOutOfRange'
| 'downloadLimited'
| 'deleteNotReady'
| 'disabled'
| 'authRequired'
| 'adminReference'
| 'errorReference'
function isZhLocale() {
return String(locale.value || '').toLowerCase().startsWith('zh')
}
function batchImageText(key: BatchImageTextKey) {
const zh: Record<BatchImageTextKey, string> = {
loadKeysFailed: '加载 API Key 失败',
loadModelsFailed: '加载可用模型失败',
loadJobsFailed: '加载批量任务失败',
selectApiKey: '请选择可用的 Gemini API Key',
noModelsForKey: '当前密钥没有可用的批量生图模型',
selectModel: '请选择模型',
promptRequired: '请至少填写一条 prompt',
submitted: '批量任务已提交',
submitFailed: '提交失败',
refreshFailed: '刷新失败',
cancelConfirm: '取消会请求上游取消;已被系统索引为成功的图片仍会按成功项结算扣费,其余冻结金额会释放。确定取消吗?',
cancelled: '已请求取消任务',
cancelFailed: '取消失败',
batchDownloadStarted: '已开始下载选中的任务',
downloadFailed: '下载失败',
retrySubmitted: '已提交失败项重试任务',
retryFailed: '重试失败项失败',
retryMissingPrompts: '这个任务没有保存失败项 prompt,无法自动重试。请复制原 prompt 后重新创建任务。',
deleteConfirm: '删除后这个任务会从你的列表隐藏,但账务记录仍会保留。确定删除吗?',
deleteSelectedConfirm: '删除后选中的任务会从你的列表隐藏,但账务记录仍会保留。确定删除吗?',
deleted: '任务记录已删除',
deleteFailed: '删除任务记录失败',
loadItemsFailed: '加载明细失败',
loadPreviewFailed: '加载图片预览失败',
copiedInstruction: '已复制批量生图说明',
loadingModels: '加载可用模型中...',
noModels: '无可用模型',
noModelsHint: '当前密钥所属分组没有配置可用于批量生图的模型。',
noCompatibleAccount: '当前密钥所属分组没有可用的批量生图上游账号。请联系管理员检查:该分组是否绑定了可调度的 Gemini API Key 或 Vertex 服务账号,以及账号是否支持所选模型。',
unsupportedProvider: '这个任务使用的批量生图通道当前不可用。请联系管理员检查批量生图通道配置。',
providerSubmitFailed: '上游批量生图任务提交失败。请联系管理员检查上游账号状态、模型权限或服务状态。',
vertexGcsBucketMissing: 'Vertex 批量生图缺少托管 GCS 存储桶配置。请联系管理员配置 BATCH_IMAGE_VERTEX_MANAGED_GCS_BUCKET 后再提交。',
queueFailed: '任务队列暂时不可用,批量任务没有成功入队。请联系管理员检查队列服务。',
billingHoldFailed: '费用冻结失败,批量任务没有成功提交。请联系管理员检查余额冻结或计费服务。',
groupDisabled: '当前密钥所属分组没有开启批量生图。你可以换一个已开启批量生图的密钥,或联系管理员开启。',
pricingMissing: '所选模型还没有配置批量生图价格。请联系管理员补充价格配置。',
insufficientBalance: '余额不足,无法冻结本次批量生图费用。',
invalidModel: '请选择一个可用于当前密钥的批量生图模型。',
invalidItems: 'Prompt 列表格式不正确,请检查是否为空、是否超过数量限制,或图片尺寸是否仍为 1K。',
duplicateCustomId: 'Prompt 列表里的 custom_id 不能重复。',
promptTooLong: '单条 prompt 过长,请缩短后重试。',
idempotencyConflict: '这次提交和之前的请求标识冲突,请刷新页面后重新提交。',
notReady: '任务还没有完成,完成后才能下载。',
outputDeleted: '这个任务的结果文件已经被清理,无法下载。',
resultMissing: '结果文件不可用,可能是上游结果文件已清理、存储权限异常,或管理员迁移过存储配置。请联系管理员检查结果文件。',
itemFailed: '这条明细没有成功图片,无法预览。',
itemImageIndexOutOfRange: '这条明细没有可预览的图片。',
downloadLimited: '当前下载请求太多,请稍后再试。',
deleteNotReady: '任务结束后才能删除记录。正在生成或结算中的任务请先等待完成。',
disabled: '批量生图功能当前未开启。',
authRequired: '当前 API Key 不可用或已失效,请重新选择密钥。',
adminReference: '请把错误码和请求 ID 发给管理员排查。',
errorReference: '错误信息',
}
const en: Record<BatchImageTextKey, string> = {
loadKeysFailed: 'Failed to load API keys.',
loadModelsFailed: 'Failed to load available models.',
loadJobsFailed: 'Failed to load batch jobs.',
selectApiKey: 'Select an available Gemini API key.',
noModelsForKey: 'This key has no available batch image models.',
selectModel: 'Select a model.',
promptRequired: 'Enter at least one prompt.',
submitted: 'Batch job submitted.',
submitFailed: 'Failed to submit the batch job.',
refreshFailed: 'Failed to refresh the job.',
cancelConfirm: 'Cancellation will be sent upstream. Images already indexed as successful will still be billed, and the remaining hold will be released. Continue?',
cancelled: 'Cancellation requested.',
cancelFailed: 'Failed to cancel the job.',
batchDownloadStarted: 'Downloads for the selected jobs have started.',
downloadFailed: 'Failed to download the result.',
retrySubmitted: 'Retry job submitted for failed items.',
retryFailed: 'Failed to retry failed items.',
retryMissingPrompts: 'This job does not have saved prompts for failed items, so it cannot be retried automatically. Recreate it with the original prompt.',
deleteConfirm: 'This hides the job from your list while keeping billing records. Delete it?',
deleteSelectedConfirm: 'This hides the selected jobs from your list while keeping billing records. Delete them?',
deleted: 'Job record deleted.',
deleteFailed: 'Failed to delete the job record.',
loadItemsFailed: 'Failed to load item details.',
loadPreviewFailed: 'Failed to load the image preview.',
copiedInstruction: 'Batch image instructions copied.',
loadingModels: 'Loading available models...',
noModels: 'No available models',
noModelsHint: 'This key’s group has no models configured for batch image generation.',
noCompatibleAccount: 'No usable upstream batch image account is available for this key’s group. Contact an administrator to check the group’s schedulable Gemini API key or Vertex service account and model support.',
unsupportedProvider: 'The batch image provider for this job is not available. Contact an administrator to check the batch image provider configuration.',
providerSubmitFailed: 'The upstream batch image job failed to submit. Contact an administrator to check the upstream account, model permission, or provider status.',
vertexGcsBucketMissing: 'Vertex batch image generation is missing the managed GCS bucket configuration. Contact an administrator to configure BATCH_IMAGE_VERTEX_MANAGED_GCS_BUCKET before submitting again.',
queueFailed: 'The task queue is temporarily unavailable, so the batch job was not queued. Contact an administrator to check the queue service.',
billingHoldFailed: 'The cost hold failed, so the batch job was not submitted. Contact an administrator to check billing or balance hold service.',
groupDisabled: 'Batch image generation is not enabled for this key’s group. Choose another enabled key or contact an administrator.',
pricingMissing: 'The selected model does not have batch image pricing configured. Contact an administrator to add pricing first.',
insufficientBalance: 'Insufficient balance to hold the estimated batch image cost.',
invalidModel: 'Select a batch image model available for the current key.',
invalidItems: 'The prompt list is invalid. Check that it is not empty, within the item limit, and still using 1K image size.',
duplicateCustomId: 'Custom IDs in the prompt list must be unique.',
promptTooLong: 'One prompt is too long. Shorten it and try again.',
idempotencyConflict: 'This submission conflicts with a previous request ID. Refresh the page and submit again.',
notReady: 'The job is not complete yet. Download will be available after completion.',
outputDeleted: 'The result files for this job have already been cleaned up.',
resultMissing: 'The result file is unavailable. It may have been cleaned up, storage permissions may be broken, or storage settings may have changed. Contact an administrator to check the result file.',
itemFailed: 'This item has no successful image to preview.',
itemImageIndexOutOfRange: 'This item has no previewable image.',
downloadLimited: 'Too many download requests are active. Please try again later.',
deleteNotReady: 'Job records can only be deleted after the job finishes.',
disabled: 'Batch image generation is currently disabled.',
authRequired: 'The current API key is unavailable or expired. Select the key again.',
adminReference: 'Send the error code and request ID to an administrator for troubleshooting.',
errorReference: 'Error detail',
}
return (isZhLocale() ? zh : en)[key]
}
function batchImageErrorReference(error: any) {
const parts: string[] = []
const code = String(error?.code || '').trim()
const requestId = String(error?.requestId || '').trim()
const status = String(error?.status || '').trim()
if (code) parts.push(isZhLocale() ? `错误码:${code}` : `code: ${code}`)
if (requestId) parts.push(isZhLocale() ? `请求 ID:${requestId}` : `request ID: ${requestId}`)
if (!code && status) parts.push(isZhLocale() ? `HTTP 状态:${status}` : `HTTP status: ${status}`)
return parts.length ? `(${parts.join(',')})` : ''
}
function batchImageAdminError(base: string, error: any) {
const reference = batchImageErrorReference(error)
return `${base}${reference ? ` ${reference}` : ''} ${batchImageText('adminReference')}`
}
function batchImagePlainError(base: string) {
return base
}
function batchImageErrorMessage(error: any, fallback: string) {
const code = String(error?.code || '').trim()
const message = String(error?.message || '').trim()
if (code === 'API_KEY_REQUIRED' || code === '401') {
return batchImagePlainError(batchImageText('authRequired'))
}
if (code === 'BATCH_IMAGE_NO_ACCOUNT_AVAILABLE' || /no compatible batch image account/i.test(message)) {
return batchImageAdminError(batchImageText('noCompatibleAccount'), error)
}
if (code === 'BATCH_IMAGE_UNSUPPORTED_PROVIDER' || /unsupported batch image provider/i.test(message)) {
return batchImageAdminError(batchImageText('unsupportedProvider'), error)
}
if (code === 'BATCH_IMAGE_VERTEX_GCS_BUCKET_MISSING' || code === 'VERTEX_MANAGED_GCS_BUCKET_MISSING') {
return batchImageAdminError(batchImageText('vertexGcsBucketMissing'), error)
}
if (
code === 'BATCH_IMAGE_PROVIDER_SUBMIT_FAILED' ||
code === 'BATCH_IMAGE_PROVIDER_MISSING_API_KEY' ||
code === 'BATCH_IMAGE_PROVIDER_MISSING_SERVICE_ACCOUNT' ||
code === 'BATCH_IMAGE_PROVIDER_UNSUPPORTED_ACCOUNT'
) {
return batchImageAdminError(batchImageText('providerSubmitFailed'), error)
}
if (code === 'BATCH_IMAGE_QUEUE_FAILED' || code === 'BATCH_IMAGE_QUEUE_NOT_CONFIGURED') {
return batchImageAdminError(batchImageText('queueFailed'), error)
}
if (code === 'BATCH_IMAGE_BILLING_HOLD_FAILED') {
return batchImageAdminError(batchImageText('billingHoldFailed'), error)
}
if (code === 'BATCH_IMAGE_GROUP_DISABLED') {
return batchImagePlainError(batchImageText('groupDisabled'))
}
if (code === 'BATCH_IMAGE_SETTLEMENT_PRICING_MISSING') {
return batchImageAdminError(batchImageText('pricingMissing'), error)
}
if (code === 'BATCH_IMAGE_INSUFFICIENT_BALANCE') {
return batchImagePlainError(batchImageText('insufficientBalance'))
}
if (code === 'BATCH_IMAGE_INVALID_MODEL') {
return batchImageText('invalidModel')
}
if (code === 'BATCH_IMAGE_INVALID_ITEMS') {
return batchImageText('invalidItems')
}
if (code === 'BATCH_IMAGE_DUPLICATE_CUSTOM_ID') {
return batchImageText('duplicateCustomId')
}
if (code === 'BATCH_IMAGE_PROMPT_TOO_LONG') {
return batchImageText('promptTooLong')
}
if (code === 'BATCH_IMAGE_IDEMPOTENCY_CONFLICT') {
return batchImagePlainError(batchImageText('idempotencyConflict'))
}
if (code === 'BATCH_IMAGE_NOT_READY') {
return batchImageText('notReady')
}
if (code === 'BATCH_IMAGE_OUTPUT_DELETED') {
return batchImageText('outputDeleted')
}
if (code === 'BATCH_IMAGE_RESULT_MISSING') {
return batchImageAdminError(batchImageText('resultMissing'), error)
}
if (code === 'BATCH_IMAGE_ITEM_FAILED') {
return batchImagePlainError(batchImageText('itemFailed'))
}
if (code === 'BATCH_IMAGE_ITEM_IMAGE_INDEX_OUT_OF_RANGE') {
return batchImagePlainError(batchImageText('itemImageIndexOutOfRange'))
}
if (code === 'BATCH_IMAGE_DOWNLOAD_LIMITED') {
return batchImageText('downloadLimited')
}
if (code === 'BATCH_IMAGE_RECORD_DELETE_NOT_READY') {
return batchImagePlainError(batchImageText('deleteNotReady'))
}
if (code === 'BATCH_IMAGE_DISABLED') {
return batchImageAdminError(batchImageText('disabled'), error)
}
if (code === 'INTERNAL_ERROR' || code === '500') {
return batchImageAdminError(fallback, error)
}
if (isZhLocale()) {
const detail = message ? `${batchImageText('errorReference')}:${message}` : batchImageText('adminReference')
return `${fallback}。${detail} ${batchImageErrorReference(error)}`
}
return message || fallback
}
function formatDate(timestamp: number) {
if (!timestamp) return ''
return new Date(timestamp * 1000).toLocaleString()
}
function defaultTaskName(timestamp?: number) {
const date = timestamp ? new Date(timestamp * 1000) : new Date()
return date.toLocaleString()
}
onMounted(() => {
void appStore.fetchPublicSettings()
void refreshPage()
void cleanupPreviewCache()
previewCacheCleanupTimer = setInterval(() => {
void cleanupPreviewCache()
}, 60 * 60 * 1000)
document.addEventListener('click', closeMoreMenu)
window.addEventListener('resize', closeMoreMenu)
window.addEventListener('scroll', closeMoreMenu, true)
window.addEventListener('resize', closePromptPopover)
window.addEventListener('scroll', closePromptPopover, true)
})
watch(
() => form.apiKeyId,
() => {
void loadAvailableModels()
},
)
onBeforeUnmount(() => {
stopPolling()
if (previewCacheCleanupTimer) {
clearInterval(previewCacheCleanupTimer)
previewCacheCleanupTimer = null
}
clearItemPreviews()
document.removeEventListener('click', closeMoreMenu)
window.removeEventListener('resize', closeMoreMenu)
window.removeEventListener('scroll', closeMoreMenu, true)
window.removeEventListener('resize', closePromptPopover)
window.removeEventListener('scroll', closePromptPopover, true)
})
</script>
<style scoped>
.batch-row-action {
display: flex !important;
flex-direction: column !important;
align-items: center !important;
justify-content: center !important;
min-width: 42px;
line-height: 1;
outline: none;
}
.batch-row-action:focus {
outline: none;
}
.batch-row-action :deep(svg) {
margin-right: 0 !important;
}
.batch-prompt-trigger:focus {
outline: none;
box-shadow: none;
}
.batch-prompt-popover {
user-select: text;
}
.batch-prompt-popover p {
scrollbar-width: thin;
}
</style>