Skip to content

DeepSeek Harness Tauri 桌面版 | Only 8mb installer, zero environment setup, preset plugins, Windows / macOS / Linux.

feat(pet): 预设宠物更新——检测 hash 更新、更新按钮、强制停用后替换安装并重新启用

hairyfd94cfa00fa4a
diff --git a/packages/dsh-tauri-pet/src/client/components/pet-settings.cssr.ts b/packages/dsh-tauri-pet/src/client/components/pet-settings.cssr.ts
index b8da76cc..777ab8ff 100644
--- a/packages/dsh-tauri-pet/src/client/components/pet-settings.cssr.ts
+++ b/packages/dsh-tauri-pet/src/client/components/pet-settings.cssr.ts
@@ -196,6 +196,12 @@ export default c([
from: { transform: 'translateX(-100%)' },
to: { transform: 'translateX(250%)' },
}),
+ c('.dshp-pet__card-actions', {
+ flex: 'none',
+ display: 'flex',
+ alignItems: 'center',
+ gap: '6px',
+ }),
c('.dshp-pet__card-action', {
flex: 'none',
appearance: 'none',
@@ -211,6 +217,10 @@ export default c([
c('&:hover:not(:disabled)', { background: 'var(--dsw-alias-interactive-bg-hover)' }),
c('&:disabled', { opacity: '0.4', cursor: 'default' }),
]),
+ // 「更新」按钮(已选/启用左侧):弱化描边次级样式,hover 同主动作。
+ c('.dshp-pet__card-action.dshp-pet__card-actionUpdate', {
+ color: 'var(--dsw-alias-label-secondary, var(--dsw-alias-label-primary))',
+ }),
c('.dshp-pet__card-action.dshp-pet__card-actionActive', {
borderColor: 'var(--dsw-alias-brand-primary)',
color: 'var(--dsw-alias-brand-primary)',
diff --git a/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx b/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx
index f06da74e..8a4d2afc 100644
--- a/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx
+++ b/packages/dsh-tauri-pet/src/client/components/pet-settings.tsx
@@ -16,10 +16,11 @@ import {
setPetEnabled,
setPetSize,
showPet,
+ updatePresetPet,
} from '../service/pet'
import { beginPetStatusFetch, commitPetStatusFetch, getPetUiSnapshot, setPetsAvailable, setPetStatus, subscribePetUi } from '../store'
import { hasAvailablePets } from '../utils/availability'
-import { progressPercent, resolvePresetCardAction } from '../utils/preset-card'
+import { progressPercent, resolvePresetCardAction, resolvePresetCardUpdate } from '../utils/preset-card'
import petSettingsStyle from './pet-settings.cssr'
/** 预设宠物下载轮询间隔(ms)。 */
@@ -43,12 +44,17 @@ interface PetCardProps {
sizeLabel?: string
thumbnail?: string
thumbnailType?: 'gif' | 'spritesheet'
+ /** 显示「更新」按钮(已安装且清单提示可更新;位于主动作左侧)。 */
+ updateDisabled?: boolean
+ updateLabel?: string
+ onUpdate?: () => void
}
function PetCard(props: PetCardProps): ReactElement {
const actionClassName = props.active
? 'dshp-pet__card-action dshp-pet__card-actionActive'
: 'dshp-pet__card-action'
+ const updateClassName = 'dshp-pet__card-action dshp-pet__card-actionUpdate'
const thumbnailClassName = props.thumbnailType === 'spritesheet'
? 'dshp-pet__card-thumb dshp-pet__card-thumbSprite'
: 'dshp-pet__card-thumb'
@@ -88,14 +94,28 @@ function PetCard(props: PetCardProps): ReactElement {
)
: null}
</span>
- <button
- type="button"
- className={actionClassName}
- disabled={props.disabled}
- onClick={props.onAction}
- >
- {props.actionLabel}
- </button>
+ <span className="dshp-pet__card-actions">
+ {props.onUpdate && props.updateLabel !== undefined
+ ? (
+ <button
+ type="button"
+ className={updateClassName}
+ disabled={props.disabled || props.updateDisabled === true}
+ onClick={props.onUpdate}
+ >
+ {props.updateLabel}
+ </button>
+ )
+ : null}
+ <button
+ type="button"
+ className={actionClassName}
+ disabled={props.disabled}
+ onClick={props.onAction}
+ >
+ {props.actionLabel}
+ </button>
+ </span>
</div>
)
}
@@ -122,6 +142,14 @@ export function presetCardAction(
return resolvePresetCardAction(item, active, progress)
}
+/** 预设宠物卡片「更新」按钮显隐(已安装且可更新,且非下载中),见 utils/preset-card。 */
+export function presetCardUpdate(
+ item: Pick<PresetPetItem, 'installed' | 'update_available' | 'phase'>,
+ progress: PresetDownloadProgress | null | undefined,
+): boolean {
+ return resolvePresetCardUpdate(item, progress)
+}
+
export function PetSettings(props: PetSettingsProps): ReactElement {
useMountStyle(petSettingsStyle, 'dsh-tauri-pet-settings-styles')
usePetLocale()
@@ -261,6 +289,30 @@ export function PetSettings(props: PetSettingsProps): ReactElement {
}
}
+ /**
+ * 更新已安装的预设宠物:走与首次下载完全相同的下载/解压流程(同一进度轮询)。
+ * 宿主侧在宠物正在使用时先强制停用,更新结束后自动重新启用(前端无需处理)。
+ */
+ async function startUpdate(id: string): Promise<void> {
+ if (busy)
+ return
+ setBusy(true)
+ setError(null)
+ try {
+ await updatePresetPet(id)
+ pollPresetDownload(id)
+ }
+ catch (updateError) {
+ if (String(updateError).includes('PET_PRESET_BUSY'))
+ pollPresetDownload(id)
+ else
+ setError(text('updateFailed'))
+ }
+ finally {
+ setBusy(false)
+ }
+ }
+
/** 启用预设宠物:选择它,并确保桌宠被唤醒(自动触发唤醒)。 */
async function enablePreset(id: string): Promise<void> {
if (busy || active === id)
@@ -383,6 +435,7 @@ export function PetSettings(props: PetSettingsProps): ReactElement {
const progress = downloads[item.id] ?? null
const action = presetCardAction(item, active, progress)
const downloading = action === 'downloading'
+ const canUpdate = resolvePresetCardUpdate(item, progress)
return (
<PetCard
key={item.id}
@@ -400,6 +453,9 @@ export function PetSettings(props: PetSettingsProps): ReactElement {
else if (action === 'download')
void startDownload(item.id)
}}
+ updateLabel={canUpdate ? text('update') : undefined}
+ updateDisabled={busy || downloading}
+ onUpdate={canUpdate ? () => { void startUpdate(item.id) } : undefined}
/>
)
})}
diff --git a/packages/dsh-tauri-pet/src/client/constants/index.ts b/packages/dsh-tauri-pet/src/client/constants/index.ts
index 75604a59..41c30283 100644
--- a/packages/dsh-tauri-pet/src/client/constants/index.ts
+++ b/packages/dsh-tauri-pet/src/client/constants/index.ts
@@ -23,6 +23,7 @@ export const CMD_IMPORT_PET = 'import_pet'
export const CMD_GET_PET_ASSET = 'get_pet_asset'
export const CMD_LIST_PRESET_PETS = 'list_preset_pets'
export const CMD_DOWNLOAD_PRESET_PET = 'download_preset_pet'
+export const CMD_UPDATE_PRESET_PET = 'update_preset_pet'
export const CMD_GET_PRESET_DOWNLOAD_PROGRESS = 'get_preset_download_progress'
export const BUILTIN_PET_ID = 'maid-deepseek-whale'
export const BUILTIN_PET_NAME = 'Maid DeepSeek Whale'
diff --git a/packages/dsh-tauri-pet/src/client/locales/index.ts b/packages/dsh-tauri-pet/src/client/locales/index.ts
index 4a29543d..6aefdce0 100644
--- a/packages/dsh-tauri-pet/src/client/locales/index.ts
+++ b/packages/dsh-tauri-pet/src/client/locales/index.ts
@@ -39,6 +39,8 @@ const DICT_ZH: Record<LocaleKey, string> = {
tabCodexDesc: '从 Codex 或压缩包中导入 Codex 宠物(支持 .zip 文件)',
tabInstalledDesc: '宠物会管理对话串,并突出显示需要关注的事项',
toggleFailed: '切换桌宠窗口失败',
+ update: '更新',
+ updateFailed: '更新预设宠物失败',
wakePet: '唤醒宠物',
}
@@ -69,6 +71,8 @@ const DICT_EN: Record<LocaleKey, string> = {
tabCodexDesc: 'Import Codex pets from Codex or archives (.zip files supported)',
tabInstalledDesc: 'Pets manage your conversation threads and highlight items that need attention',
toggleFailed: 'Failed to toggle the pet window',
+ update: 'Update',
+ updateFailed: 'Failed to update preset pet',
wakePet: 'Wake pet',
}
diff --git a/packages/dsh-tauri-pet/src/client/service/pet.ts b/packages/dsh-tauri-pet/src/client/service/pet.ts
index d823eefc..5eedce16 100644
--- a/packages/dsh-tauri-pet/src/client/service/pet.ts
+++ b/packages/dsh-tauri-pet/src/client/service/pet.ts
@@ -13,6 +13,7 @@ import {
CMD_SET_PET_ENABLED,
CMD_SET_PET_SIZE,
CMD_SHOW_PET,
+ CMD_UPDATE_PRESET_PET,
} from '../constants'
export function fetchPetStatus(): Promise<PetStatus> {
@@ -61,6 +62,14 @@ export function downloadPresetPet(id: string): Promise<void> {
return invokeBridgedTauri<void>(CMD_DOWNLOAD_PRESET_PET, { id })
}
+/**
+ * 更新已安装的预设宠物(后台执行;进度用 fetchPresetDownloadProgress 轮询)。
+ * 若宠物正在使用,宿主会先强制停用,更新结束后自动重新启用。
+ */
+export function updatePresetPet(id: string): Promise<void> {
+ return invokeBridgedTauri<void>(CMD_UPDATE_PRESET_PET, { id })
+}
+
/** 查询预设宠物下载进度。 */
export function fetchPresetDownloadProgress(id: string): Promise<PresetDownloadProgress> {
return invokeBridgedTauri<PresetDownloadProgress>(CMD_GET_PRESET_DOWNLOAD_PROGRESS, { id })
diff --git a/packages/dsh-tauri-pet/src/client/types/index.ts b/packages/dsh-tauri-pet/src/client/types/index.ts
index 42621d2d..60e01369 100644
--- a/packages/dsh-tauri-pet/src/client/types/index.ts
+++ b/packages/dsh-tauri-pet/src/client/types/index.ts
@@ -32,6 +32,8 @@ export interface PresetPetItem {
installed: boolean
name: string
size_mb?: number | null
+ /** 已安装且清单 ref 与安装记录不同(或无记录)→ 可更新。 */
+ update_available?: boolean
/** 当前下载阶段(idle|downloading|extracting|done|failed),跨挂载恢复下载中视图用。 */
phase?: PresetDownloadProgress['phase'] | null
}
@@ -111,4 +113,6 @@ export type LocaleKey
| 'tabCodexDesc'
| 'tabInstalledDesc'
| 'toggleFailed'
+ | 'update'
+ | 'updateFailed'
| 'wakePet'
diff --git a/packages/dsh-tauri-pet/src/client/utils/preset-card.test.ts b/packages/dsh-tauri-pet/src/client/utils/preset-card.test.ts
index 1ba9f008..c3e9246c 100644
--- a/packages/dsh-tauri-pet/src/client/utils/preset-card.test.ts
+++ b/packages/dsh-tauri-pet/src/client/utils/preset-card.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest'
-import { progressPercent, resolvePresetCardAction } from './preset-card'
+import { progressPercent, resolvePresetCardAction, resolvePresetCardUpdate } from './preset-card'
function progress(phase: PresetDownloadPhase, received = 0, total = 0): { phase: PresetDownloadPhase, received: number, total: number, error?: string | null } {
return { phase, received, total }
@@ -45,6 +45,29 @@ describe('resolvePresetCardAction', () => {
})
})
+describe('resolvePresetCardUpdate', () => {
+ const maid = { id: 'maid-deepseek-whale', installed: true, update_available: true, phase: 'idle' as const }
+
+ it('已安装且可更新 → 显示更新按钮', () => {
+ expect(resolvePresetCardUpdate(maid, null)).toBe(true)
+ expect(resolvePresetCardUpdate(maid, progress('done'))).toBe(true)
+ })
+
+ it('未安装 / 清单未提示更新 → 不显示', () => {
+ expect(resolvePresetCardUpdate({ ...maid, installed: false }, null)).toBe(false)
+ expect(resolvePresetCardUpdate({ ...maid, update_available: false }, null)).toBe(false)
+ expect(resolvePresetCardUpdate({ ...maid, update_available: undefined }, null)).toBe(false)
+ })
+
+ it('下载/解压中 → 隐藏更新按钮(避免与替换安装冲突)', () => {
+ expect(resolvePresetCardUpdate(maid, progress('downloading'))).toBe(false)
+ expect(resolvePresetCardUpdate(maid, progress('extracting'))).toBe(false)
+ // 清单 phase 兜底(跨挂载恢复下载中视图)。
+ expect(resolvePresetCardUpdate({ ...maid, phase: 'downloading' }, null)).toBe(false)
+ expect(resolvePresetCardUpdate({ ...maid, phase: 'extracting' }, null)).toBe(false)
+ })
+})
+
describe('progressPercent', () => {
it('已知总量返回截断百分比', () => {
expect(progressPercent(progress('downloading', 50, 200))).toBe(25)
diff --git a/packages/dsh-tauri-pet/src/client/utils/preset-card.ts b/packages/dsh-tauri-pet/src/client/utils/preset-card.ts
index f78676d0..9cf6040c 100644
--- a/packages/dsh-tauri-pet/src/client/utils/preset-card.ts
+++ b/packages/dsh-tauri-pet/src/client/utils/preset-card.ts
@@ -29,6 +29,20 @@ export function resolvePresetCardAction(
return 'download'
}
+/**
+ * 是否显示「更新」按钮:已安装、清单提示可更新、且不在下载/解压中。
+ * 更新按钮位于主动作(已选/启用)左侧;下载中时隐藏,避免与替换安装冲突。
+ */
+export function resolvePresetCardUpdate(
+ item: Pick<PresetPetItem, 'installed' | 'update_available' | 'phase'>,
+ progress: PresetDownloadProgress | null | undefined,
+): boolean {
+ if (!item.installed || item.update_available !== true)
+ return false
+ const phase = progress?.phase ?? item.phase
+ return phase !== 'downloading' && phase !== 'extracting'
+}
+
/** 把下载进度渲染成百分比;未知总量(下载早期/解压中)返回 null 表示不确定进度。 */
export function progressPercent(progress: PresetDownloadProgress): number | null {
if (progress.total > 0)
diff --git a/src-tauri/src/bridge/preset_pet.rs b/src-tauri/src/bridge/preset_pet.rs
index 300d497e..927f3cfd 100644
--- a/src-tauri/src/bridge/preset_pet.rs
+++ b/src-tauri/src/bridge/preset_pet.rs
@@ -70,7 +70,12 @@ struct PresetPetSpec {
size_mb: Option<f64>,
}
-/// 设置页可展示的清单条目(含本机安装状态与进行中的下载阶段)。
+/// 已安装预设宠物的版本记录文件名(安装目录根下,记录安装时的 ref)。
+/// 设置页据此判断「是否有更新」:清单 ref 与安装记录不一致(或旧安装无记录)
+/// 时给出更新入口。
+const PRESET_REF_FILE: &str = ".preset-ref";
+
+/// 设置页可展示的清单条目(含本机安装状态、版本更新提示与进行中的下载阶段)。
#[derive(Debug, Clone, Serialize)]
pub struct PresetPetListItem {
pub id: String,
@@ -79,6 +84,9 @@ pub struct PresetPetListItem {
pub image: Option<String>,
pub size_mb: Option<f64>,
pub installed: bool,
+ /// 已安装且清单 ref 与安装记录不同(或无记录)→ 可更新。
+ /// 清单未固定 ref(跟随 main)时恒为 false,无法检测差异。
+ pub update_available: bool,
/// 当前下载阶段(idle | downloading | extracting | done | failed)。
/// 设置页跨挂载恢复「下载中」视图用:返回应用再进设置时,进程内注册表仍保留
/// 进行中的下载阶段,前端据此显示进度条并自动恢复轮询。
@@ -167,6 +175,44 @@ fn installed_dir(root: &Path, id: &str) -> PathBuf {
root.join(id)
}
+/// 清单条目实际下载/安装所用的引用:固定 ref,缺省回退 `main`。
+fn preset_reference(spec: &PresetPetSpec) -> String {
+ spec.r#ref
+ .clone()
+ .filter(|value| !value.is_empty())
+ .unwrap_or_else(|| "main".to_string())
+}
+
+/// 读取已安装宠物目录的版本记录(`.preset-ref`);缺失或内容为空白返回 None。
+/// 旧安装(记录功能上线前下载的)无此文件,视为「未知版本」而非「最新」。
+fn read_installed_ref(dir: &Path) -> Option<String> {
+ let value = fs::read_to_string(dir.join(PRESET_REF_FILE)).ok()?;
+ let trimmed = value.trim();
+ (!trimmed.is_empty()).then(|| trimmed.to_string())
+}
+
+/// 在安装目录写入 ref 版本记录(安装/更新成功后调用,供后续更新检测)。
+fn write_installed_ref(dir: &Path, reference: &str) -> Result<(), String> {
+ fs::write(dir.join(PRESET_REF_FILE), reference).map_err(|error| {
+ format!(
+ "PET_PRESET_REF_WRITE_FAILED: failed to write {}: {error}",
+ dir.join(PRESET_REF_FILE).display()
+ )
+ })
+}
+
+/// 是否可更新:已安装,且清单 ref 与安装记录不一致(或无记录)。
+/// 清单未固定 ref(跟随 main 分支)时无法判断差异,恒为 false。
+fn preset_update_available(installed: bool, catalog_ref: Option<&str>, installed_ref: Option<&str>) -> bool {
+ if !installed {
+ return false;
+ }
+ match catalog_ref.filter(|value| !value.is_empty()) {
+ None => false,
+ Some(catalog) => installed_ref.as_deref() != Some(catalog),
+ }
+}
+
pub(crate) fn safe_preset_id(id: &str) -> bool {
!id.is_empty()
&& id.len() <= 64
@@ -200,11 +246,7 @@ fn parse_repo_owner(url: &str) -> Result<(String, String), String> {
/// 构造仓库 tarball 下载地址(codeload 直连 + ghfast.top 镜像兜底)。
fn tarball_urls(spec: &PresetPetSpec) -> Result<Vec<String>, String> {
let (owner, repo) = parse_repo_owner(&spec.repo)?;
- let reference = spec
- .r#ref
- .clone()
- .filter(|value| !value.is_empty())
- .unwrap_or_else(|| "main".to_string());
+ let reference = preset_reference(spec);
if reference.contains('/') || reference.contains('\\') || reference.contains("..") {
return Err("PET_PRESET_REF_INVALID: ref must be a plain commit or branch name".to_string());
}
@@ -574,8 +616,48 @@ async fn download_tarball(
Err(last_error)
}
-/// 后台安装流程:下载 → 解压 assets → 校验 → staging 原子 rename。
-async fn run_preset_download(app: &AppHandle, spec: PresetPetSpec) -> Result<(), String> {
+/// 把 staging 安装到 target:
+/// - 首次安装(target 不存在)直接 rename;
+/// - 更新安装(target 存在且 `replace`):两步 rename(旧目录→备份、staging→target)
+/// 原子换新,任一步失败回滚备份,成功后清理备份目录。
+fn install_staging(
+ root: &Path,
+ id: &str,
+ nonce: u128,
+ staging: &Path,
+ target: &Path,
+ replace: bool,
+) -> Result<(), String> {
+ if !target.exists() {
+ return fs::rename(staging, target).map_err(|error| {
+ format!("PET_PRESET_INSTALL_FAILED: failed to install preset pet: {error}")
+ });
+ }
+ if !replace {
+ return Err(format!(
+ "PET_PRESET_ALREADY_INSTALLED: preset pet {} is already installed",
+ id
+ ));
+ }
+ let backup = root.join(format!(".preset-backup-{id}-{nonce}"));
+ fs::rename(target, &backup).map_err(|error| {
+ format!(
+ "PET_PRESET_INSTALL_FAILED: failed to back up existing preset pet: {error}"
+ )
+ })?;
+ if let Err(error) = fs::rename(staging, target) {
+ // 新目录就位失败:回滚备份,保留旧版本完整可用(宁可更新失败也不丢宠物)。
+ let _ = fs::rename(&backup, target);
+ return Err(format!(
+ "PET_PRESET_INSTALL_FAILED: failed to install updated preset pet: {error}"
+ ));
+ }
+ let _ = fs::remove_dir_all(&backup);
+ Ok(())
+}
+
+/// 后台安装流程:下载 → 解压 assets → 校验 → staging 原子安装(首次或替换)。
+async fn run_preset_download(app: &AppHandle, spec: PresetPetSpec, replace: bool) -> Result<(), String> {
let root = preset_pets_root(app);
fs::create_dir_all(&root).map_err(|error| {
format!("PET_PRESET_DIR_FAILED: failed to create pets directory: {error}")
@@ -596,7 +678,7 @@ async fn run_preset_download(app: &AppHandle, spec: PresetPetSpec) -> Result<(),
.map(|mb| (mb * 1024.0 * 1024.0).round() as u64)
.unwrap_or(0);
log::info!(
- "[preset-pet] install {}: start (assets={:?}, estimated {estimated_total} bytes)",
+ "[preset-pet] install {}: start (assets={:?}, estimated {estimated_total} bytes, replace={replace})",
spec.id,
spec.assets
);
@@ -628,15 +710,9 @@ async fn run_preset_download(app: &AppHandle, spec: PresetPetSpec) -> Result<(),
log::info!("[preset-pet] install {}: tarball extracted", spec.id);
validate_preset_config(&staging)?;
let target = installed_dir(&root, &spec.id);
- if target.exists() {
- return Err(format!(
- "PET_PRESET_ALREADY_INSTALLED: preset pet {} is already installed",
- spec.id
- ));
- }
- fs::rename(&staging, &target).map_err(|error| {
- format!("PET_PRESET_INSTALL_FAILED: failed to install preset pet: {error}")
- })?;
+ install_staging(&root, &spec.id, nonce, &staging, &target, replace)?;
+ // 版本记录:供设置页检测更新(清单 ref 与记录不一致时提示可更新)。
+ write_installed_ref(&target, &preset_reference(&spec))?;
log::info!(
"[preset-pet] install {}: installed at {}",
spec.id,
@@ -653,7 +729,7 @@ async fn run_preset_download(app: &AppHandle, spec: PresetPetSpec) -> Result<(),
result
}
-/// 列出预设宠物清单(含安装状态)。
+/// 列出预设宠物清单(含安装状态与版本更新提示)。
#[tauri::command]
pub fn list_preset_pets(app: AppHandle) -> Result<Vec<PresetPetListItem>, String> {
let catalog = read_preset_catalog(&app)?;
@@ -674,8 +750,11 @@ pub fn list_preset_pets(app: AppHandle) -> Result<Vec<PresetPetListItem>, String
));
}
let phase = get_preset_progress(&spec.id).phase;
+ let installed = installed_dir(&root, &spec.id).is_dir();
+ let installed_ref = installed.then(|| read_installed_ref(&installed_dir(&root, &spec.id))).flatten();
items.push(PresetPetListItem {
- installed: installed_dir(&root, &spec.id).is_dir(),
+ installed,
+ update_available: preset_update_available(installed, spec.r#ref.as_deref(), installed_ref.as_deref()),
id: spec.id,
name: spec.name,
desc: spec.desc,
@@ -714,7 +793,73 @@ pub fn download_preset_pet(app: AppHandle, id: String) -> Result<(), String> {
);
let app = app.clone();
tauri::async_runtime::spawn(async move {
- let progress = match run_preset_download(&app, spec).await {
+ let progress = match run_preset_download(&app, spec, false).await {
+ Ok(()) => PresetDownloadProgress {
+ phase: "done".to_string(),
+ received: 0,
+ total: 0,
+ error: None,
+ },
+ Err(error) => PresetDownloadProgress {
+ phase: "failed".to_string(),
+ received: 0,
+ total: 0,
+ error: Some(error),
+ },
+ };
+ set_preset_progress(&id, progress);
+ });
+ Ok(())
+}
+
+/// 更新已安装的预设宠物(后台执行,立即返回;进度用轮询查询)。
+///
+/// 需求语义:
+/// 1. 仅对「已安装」开放(未安装走 `download_preset_pet`);
+/// 2. 若该宠物正在使用(激活且启用)则先强制停用(隐藏桌宠窗口),避免更新
+/// 替换媒体文件时窗口仍在按旧 URL 流式读取;
+/// 3. 走与首次下载完全相同的下载/解压/校验流程(替换模式:旧目录备份 → 新目录
+/// 原子换入,失败回滚保旧版本可用),完成后写 `.preset-ref` 版本记录;
+/// 4. 更新结束(无论成败)后若该宠物此前正在使用,重新启用桌宠。若失败则
+/// 旧版本仍可用,重新启用后食用旧版本(下一次更新再试)。
+#[tauri::command]
+pub fn update_preset_pet(app: AppHandle, id: String) -> Result<(), String> {
+ let id = id.trim().to_string();
+ let catalog = read_preset_catalog(&app)?;
+ let spec = catalog
+ .into_iter()
+ .find(|entry| entry.id == id)
+ .ok_or_else(|| format!("PET_PRESET_NOT_FOUND: preset pet {id} is not in the catalog"))?;
+ let root = preset_pets_root(&app);
+ if !installed_dir(&root, &id).is_dir() {
+ return Err(format!("PET_PRESET_NOT_INSTALLED: preset pet {id} is not installed"));
+ }
+ if get_preset_progress(&id).phase == "downloading" || get_preset_progress(&id).phase == "extracting" {
+ return Err(format!("PET_PRESET_BUSY: preset pet {id} is already downloading"));
+ }
+
+ // 快照「是否正在使用该宠物」:激活 id 相同且桌宠启用。使用中则先强制停用,
+ // 更新结束后恢复启用(无论成败:失败时旧版本仍在,继续可用)。
+ let status = crate::bridge::pet::get_pet_status(app.clone());
+ let was_active = status.active_pet == id;
+ let was_enabled = status.enabled && was_active;
+ if was_enabled {
+ log::info!("[preset-pet] update {id}: pet in use, disabling pet window during update");
+ crate::bridge::pet::set_pet_enabled(app.clone(), false)?;
+ }
+
+ set_preset_progress(
+ &id,
+ PresetDownloadProgress {
+ phase: "downloading".to_string(),
+ received: 0,
+ total: 0,
+ error: None,
+ },
+ );
+ let app = app.clone();
+ tauri::async_runtime::spawn(async move {
+ let progress = match run_preset_download(&app, spec, true).await {
Ok(()) => PresetDownloadProgress {
phase: "done".to_string(),
received: 0,
@@ -728,7 +873,21 @@ pub fn download_preset_pet(app: AppHandle, id: String) -> Result<(), String> {
error: Some(error),
},
};
+ let next_phase = progress.phase.clone();
set_preset_progress(&id, progress);
+ if next_phase == "done" {
+ // 资源换新后重载桌宠窗口:URL 不变但文件已替换,WebView 缓存可能命中
+ // 旧 webm。哪怕当前未启用(窗口隐藏)也重载——页面重新加载后按
+ // get_pet_status 维持隐藏状态,下次启用时直接呈现新资源。
+ crate::desktop::pet::reload_pet_window(&app);
+ }
+ // 更新前在用 → 重新启用桌宠窗口(新文件已就位,或旧版本被回滚后继续可用)。
+ if was_enabled {
+ log::info!("[preset-pet] update {id}: re-enabling pet window after update");
+ if let Err(error) = crate::bridge::pet::set_pet_enabled(app.clone(), true) {
+ log::warn!("[preset-pet] update {id}: failed to re-enable pet window: {error}");
+ }
+ }
});
Ok(())
}
@@ -1589,4 +1748,88 @@ mod tests {
let parsed: Value = serde_json::from_str(&stripped).expect("valid JSON");
assert_eq!(parsed["url"], "https://example.com/a//b");
}
+
+ #[test]
+ fn installed_ref_round_trips_and_handles_missing() {
+ let directory = TestDirectory::new("ref");
+ fs::create_dir_all(&directory.0).unwrap();
+ // 无记录 → None(旧安装视为未知版本,不误判为最新)。
+ assert_eq!(read_installed_ref(&directory.0), None);
+ // 写入后可读回;空白内容视为无记录。
+ write_installed_ref(&directory.0, "e1ff8c1e4001878cbb80441262d530e16541f138").unwrap();
+ assert_eq!(
+ read_installed_ref(&directory.0).as_deref(),
+ Some("e1ff8c1e4001878cbb80441262d530e16541f138")
+ );
+ fs::write(directory.0.join(PRESET_REF_FILE), " ").unwrap();
+ assert_eq!(read_installed_ref(&directory.0), None);
+ }
+
+ #[test]
+ fn update_available_requires_installed_and_differs_from_catalog_ref() {
+ // 未安装 → 不可更新(走下载入口)。
+ assert!(!preset_update_available(false, Some("abc"), Some("abc")));
+ assert!(!preset_update_available(false, Some("abc"), None));
+ // 已安装且清单未固定 ref(跟随 main)→ 无法检测差异,不可更新。
+ assert!(!preset_update_available(true, None, Some("abc")));
+ assert!(!preset_update_available(true, None, None));
+ // 已安装且 ref 与安装记录一致 → 最新,不可更新。
+ assert!(!preset_update_available(true, Some("abc"), Some("abc")));
+ // 已安装且 ref 不一致 → 可更新。
+ assert!(preset_update_available(true, Some("abc"), Some("def")));
+ // 已安装但无安装记录(升级前下载的旧安装)→ 无法确认版本,视为可更新。
+ assert!(preset_update_available(true, Some("abc"), None));
+ }
+
+ #[test]
+ fn install_staging_replaces_existing_with_backup_and_rolls_back_on_failure() {
+ let directory = TestDirectory::new("replace");
+ let root = directory.0.join("root");
+ fs::create_dir_all(&root).unwrap();
+ let target = root.join("pets");
+ let staging = root.join("staging");
+ fs::create_dir_all(&target).unwrap();
+ fs::write(target.join("old-config.jsonc"), b"old").unwrap();
+ fs::create_dir_all(&staging).unwrap();
+ fs::write(staging.join("new-config.jsonc"), b"new").unwrap();
+
+ // 替换:旧目录被换出,新目录就位,备份被清理。
+ let nonce = 100_u128;
+ install_staging(&root, "pets", nonce, &staging, &target, true).unwrap();
+ assert!(target.join("new-config.jsonc").is_file());
+ assert!(!target.join("old-config.jsonc").exists());
+ assert!(!root.join(".preset-backup-pets-100").exists());
+
+ // 替换失败(staging 缺失)→ 回滚备份,旧版本完整保留。
+ let bad_staging = root.join("bad-staging");
+ let err = install_staging(&root, "pets", nonce + 1, &bad_staging, &target, true).unwrap_err();
+ assert!(err.starts_with("PET_PRESET_INSTALL_FAILED:"));
+ assert!(target.join("new-config.jsonc").is_file(), "回滚后保留新版本");
+ assert!(!root.join(".preset-backup-pets-101").exists(), "备份回滚后不残留");
+ }
+
+ #[test]
+ fn install_staging_first_install_and_non_replace_reject() {
+ let directory = TestDirectory::new("fresh");
+ let root = directory.0.join("root");
+ fs::create_dir_all(&root).unwrap();
+ let target = root.join("fresh-pet");
+ let staging = root.join("staging");
+ fs::create_dir_all(&staging).unwrap();
+ fs::write(staging.join("config.jsonc"), b"{}").unwrap();
+
+ // 首次安装:target 不存在 → 直接 rename。
+ install_staging(&root, "fresh-pet", 1, &staging, &target, false).unwrap();
+ assert!(target.join("config.jsonc").is_file());
+
+ // 非替换模式下 target 已存在 → 拒绝;staging 保留,由调用方(run_preset_download
+ // 失败路径 remove_dir_all)清理,install_staging 自身不动它。
+ let staging2 = root.join("staging2");
+ fs::create_dir_all(&staging2).unwrap();
+ fs::write(staging2.join("config.jsonc"), b"{}").unwrap();
+ let err = install_staging(&root, "fresh-pet", 2, &staging2, &target, false).unwrap_err();
+ assert!(err.starts_with("PET_PRESET_ALREADY_INSTALLED:"));
+ assert!(target.join("config.jsonc").is_file());
+ assert!(staging2.join("config.jsonc").is_file(), "staging 保留等待调用方清理");
+ }
}
diff --git a/src-tauri/src/desktop/builder.rs b/src-tauri/src/desktop/builder.rs
index ddb7eed7..aacfe070 100644
--- a/src-tauri/src/desktop/builder.rs
+++ b/src-tauri/src/desktop/builder.rs
@@ -599,6 +599,7 @@ pub fn handler() -> impl Fn(Invoke<Wry>) -> bool + Send + Sync + 'static {
crate::bridge::preset_pet::get_preset_pet_assets,
crate::bridge::list_preset_pets,
crate::bridge::download_preset_pet,
+ crate::bridge::update_preset_pet,
crate::bridge::get_preset_download_progress,
crate::desktop::pet_mouse::start_pet_mouse_stream,
]
diff --git a/src-tauri/src/desktop/pet.rs b/src-tauri/src/desktop/pet.rs
index e81c9d5d..5dc7a6dd 100644
--- a/src-tauri/src/desktop/pet.rs
+++ b/src-tauri/src/desktop/pet.rs
@@ -420,6 +420,17 @@ pub fn init_pet_window<R: Runtime>(app: &AppHandle<R>) {
}
}
+/// 重载桌宠窗口页面(预设宠物更新/替换后调用)。
+///
+/// 桌宠窗口按 `activePet` 只拉取一次协议资源(config + webm manifest),
+/// 更新换入新文件后 URL 不变,WebView 可能继续命中缓存里的旧 webm;
+/// 显式 reload 让新资源立即生效。窗口隐藏时 reload 同样安全(页面本身常驻)。
+pub fn reload_pet_window<R: Runtime>(app: &AppHandle<R>) {
+ if let Some(window) = app.get_webview_window(PET_WINDOW_LABEL) {
+ let _ = window.eval("location.reload()");
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
diff --git a/src/hooks/use-iframe-invoke.ts b/src/hooks/use-iframe-invoke.ts
index 872dbaea..098fdffa 100644
--- a/src/hooks/use-iframe-invoke.ts
+++ b/src/hooks/use-iframe-invoke.ts
@@ -47,6 +47,7 @@ const ALLOWED_INVOKE_CMDS = new Set([
'get_pet_asset',
'list_preset_pets',
'download_preset_pet',
+ 'update_preset_pet',
'get_preset_download_progress',
])