139 lines
4.5 KiB
JavaScript
139 lines
4.5 KiB
JavaScript
const fs = require('fs')
|
||
const base = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/'
|
||
function patch(file, pairs) {
|
||
const p = base + file
|
||
let c = fs.readFileSync(p, 'utf8')
|
||
for (const [from, to] of pairs) {
|
||
if (!c.includes(from)) {
|
||
console.log('MISS in', file, '::', from.slice(0, 70).replace(/\n/g, '\\n'))
|
||
continue
|
||
}
|
||
c = c.replace(from, to)
|
||
}
|
||
fs.writeFileSync(p, c, 'utf8')
|
||
}
|
||
|
||
// ---- 存档字段:预设轮换状态
|
||
patch('shared/petState.ts', [
|
||
[
|
||
` /** 活动日志(最近的在前,最多 60 条) */
|
||
log: string[]`,
|
||
` /** 活动日志(最近的在前,最多 60 条) */
|
||
log: string[]
|
||
/** 预设对话轮换:cycleAt 到期刷新,used 表示已选过 */
|
||
presets?: { cycleAt: number; ids: string[]; used: string[]; flavor: string }`
|
||
]
|
||
])
|
||
|
||
// ---- store:预设轮换逻辑
|
||
patch('main/stateStore.ts', [
|
||
[
|
||
"import { ACHIEVEMENTS } from '../shared/achievements'",
|
||
"import { ACHIEVEMENTS } from '../shared/achievements'\nimport { presetsFor, type PresetDialogue } from '../shared/dialogue'\n\nconst PRESET_CYCLE_MS = 30 * 60 * 1000"
|
||
],
|
||
[
|
||
` setBirthday(day: string): ActionResult {`,
|
||
` /** 取当前可用预设(到期或换风格则重新随机一组,5~7 条) */
|
||
getAvailablePresets(flavor: string, now = Date.now()): { presets: PresetDialogue[]; nextAt: number } {
|
||
const pool = presetsFor(flavor)
|
||
const cur = this.save.presets
|
||
if (!cur || now >= cur.cycleAt || cur.flavor !== flavor || !Array.isArray(cur.ids)) {
|
||
const count = 5 + Math.floor(Math.random() * 3)
|
||
const ids = [...pool.map((p) => p.id)]
|
||
for (let i = ids.length - 1; i > 0; i--) {
|
||
const j = Math.floor(Math.random() * (i + 1))
|
||
;[ids[i], ids[j]] = [ids[j], ids[i]]
|
||
}
|
||
this.save.presets = {
|
||
cycleAt: now + PRESET_CYCLE_MS,
|
||
ids: ids.slice(0, Math.min(count, ids.length)),
|
||
used: [],
|
||
flavor
|
||
}
|
||
this.saveNow()
|
||
}
|
||
const st = this.save.presets
|
||
const remaining = st.ids
|
||
.filter((id) => !st.used.includes(id))
|
||
.map((id) => pool.find((p) => p.id === id))
|
||
.filter((p): p is PresetDialogue => Boolean(p))
|
||
return { presets: remaining, nextAt: st.cycleAt }
|
||
}
|
||
|
||
markPresetUsed(id: string): void {
|
||
const st = this.save.presets
|
||
if (!st) return
|
||
if (!st.used.includes(id)) st.used.push(id)
|
||
this.saveNow()
|
||
}
|
||
|
||
setBirthday(day: string): ActionResult {`
|
||
]
|
||
])
|
||
|
||
// ---- main:IPC
|
||
patch('main/index.ts', [
|
||
[
|
||
` ipcMain.handle('pet:chat:preset', (_e, id: string) => {`,
|
||
` ipcMain.handle('pet:chat:presets', () => store.getAvailablePresets(currentFlavor()))
|
||
ipcMain.handle('pet:chat:preset', (_e, id: string) => {`
|
||
],
|
||
[
|
||
` store.applyChatEffect(good ? preset.good : preset.bad)`,
|
||
` store.applyChatEffect(good ? preset.good : preset.bad)\n store.markPresetUsed(String(id))`
|
||
],
|
||
[
|
||
` return { ok: true, message: (good ? '🎲 ' : '💧 ') + (good ? preset.good.toast : preset.bad.toast), reply }`,
|
||
` const { presets } = store.getAvailablePresets(currentFlavor())
|
||
return {
|
||
ok: true,
|
||
message: (good ? '🎲 ' : '💧 ') + (good ? preset.good.toast : preset.bad.toast),
|
||
reply,
|
||
presets: presets.map((p) => ({
|
||
id: p.id,
|
||
text: p.text,
|
||
textNormal: p.textNormal,
|
||
kind: p.kind
|
||
}))
|
||
}`
|
||
]
|
||
])
|
||
|
||
// ---- preload / api
|
||
patch('preload/index.ts', [
|
||
[
|
||
" chatPreset: (id: string) => Promise<{ ok: boolean; message: string; reply?: string }>",
|
||
` chatPreset: (id: string) => Promise<{
|
||
ok: boolean
|
||
message: string
|
||
reply?: string
|
||
presets?: Array<{ id: string; text: string; textNormal: string; kind: string }>
|
||
}>
|
||
getChatPresets: () => Promise<{
|
||
presets: Array<{ id: string; text: string; textNormal: string; kind: string }>
|
||
nextAt: number
|
||
}>`
|
||
],
|
||
[
|
||
" chatPreset: (id: string) => ipcRenderer.invoke('pet:chat:preset', id),",
|
||
" chatPreset: (id: string) => ipcRenderer.invoke('pet:chat:preset', id),\n getChatPresets: () => ipcRenderer.invoke('pet:chat:presets'),"
|
||
]
|
||
])
|
||
patch('renderer/src/api.ts', [
|
||
[
|
||
" chatPreset: (id: string) => Promise<{ ok: boolean; message: string; reply?: string }>",
|
||
` chatPreset: (id: string) => Promise<{
|
||
ok: boolean
|
||
message: string
|
||
reply?: string
|
||
presets?: Array<{ id: string; text: string; textNormal: string; kind: string }>
|
||
}>
|
||
getChatPresets: () => Promise<{
|
||
presets: Array<{ id: string; text: string; textNormal: string; kind: string }>
|
||
nextAt: number
|
||
}>`
|
||
]
|
||
])
|
||
|
||
console.log('preset rotation patched')
|