168 lines
6.3 KiB
JavaScript
168 lines
6.3 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')
|
||
}
|
||
|
||
// ---- ChatBar:角色名占位 + 风格化预设
|
||
patch('renderer/src/chat/ChatBar.tsx', [
|
||
[
|
||
"import { suggestions } from '../../../shared/dialogue'",
|
||
"import { presetsFor, suggestions, type PresetDialogue } from '../../../shared/dialogue'"
|
||
],
|
||
[
|
||
"const PRESETS = ['今天在做什么?', '讲个化学小知识吧', '你饿不饿?', '被夸会脸红吗?', '你喜欢小氯吗?', '带我看看你的记录本']",
|
||
"const PRESETS_CHEM = ['今天在做什么?', '讲个化学小知识吧', '你饿不饿?', '被夸会脸红吗?', '你喜欢小氯吗?', '带我看看你的记录本']\nconst PRESETS_NORMAL = ['今天在做什么?', '给我讲个笑话吧', '你饿不饿?', '被夸会脸红吗?', '你最喜欢什么?', '今天心情怎么样?']"
|
||
],
|
||
[
|
||
" const [state, setState] = useState<PetSave | null>(null)",
|
||
" const [state, setState] = useState<PetSave | null>(null)\n const [flavor, setFlavor] = useState<'chemistry' | 'normal'>('chemistry')"
|
||
],
|
||
[
|
||
` const applyTheme = (c: { theme: string; accent: string }): void => {
|
||
document.documentElement.dataset.theme = c.theme
|
||
document.documentElement.style.setProperty('--accent', c.accent)
|
||
}`,
|
||
` const applyTheme = (c: { theme: string; accent: string; currentSkin?: string; flavorMode?: string }): void => {
|
||
document.documentElement.dataset.theme = c.theme
|
||
document.documentElement.style.setProperty('--accent', c.accent)
|
||
const mode = c.flavorMode || 'auto'
|
||
if (mode === 'chemistry') setFlavor('chemistry')
|
||
else if (mode === 'normal') setFlavor('normal')
|
||
else {
|
||
void fetch('pet://skins/' + (c.currentSkin || 'xiaona') + '/skin.json')
|
||
.then((r) => (r.ok ? r.json() : null))
|
||
.then((m) => setFlavor(m && m.flavor === 'normal' ? 'normal' : 'chemistry'))
|
||
.catch(() => setFlavor('chemistry'))
|
||
}
|
||
}`
|
||
],
|
||
[
|
||
" const list = state ? suggestions(state) : PRESETS",
|
||
` const list = state ? suggestions(state) : flavor === 'normal' ? PRESETS_NORMAL : PRESETS_CHEM
|
||
const presets: PresetDialogue[] = presetsFor(flavor)`
|
||
],
|
||
// placeholder 用角色名
|
||
[
|
||
` placeholder="和小钠说…"`,
|
||
` placeholder={'和' + (state?.name ?? '她') + '说…'}`
|
||
],
|
||
// 预设列表:普通话题 + 带效果的互动预设
|
||
[
|
||
` {open && (
|
||
<div className="chat-suggest">
|
||
{list.map((s) => (
|
||
<button
|
||
key={s}
|
||
className="chat-suggest-item"
|
||
onClick={() => {
|
||
setText(s)
|
||
setOpen(false)
|
||
}}
|
||
>
|
||
{s}
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}`,
|
||
` {open && (
|
||
<div className="chat-suggest">
|
||
{presets.map((p) => (
|
||
<button
|
||
key={p.id}
|
||
className="chat-suggest-item"
|
||
onClick={() => {
|
||
setOpen(false)
|
||
void api()?.chatPreset(p.id)
|
||
}}
|
||
>
|
||
🎲 {flavor === 'normal' ? p.textNormal : p.text}
|
||
</button>
|
||
))}
|
||
{list.map((s) => (
|
||
<button
|
||
key={s}
|
||
className="chat-suggest-item"
|
||
onClick={() => {
|
||
setText(s)
|
||
setOpen(false)
|
||
}}
|
||
>
|
||
{s}
|
||
</button>
|
||
))}
|
||
</div>
|
||
)}`
|
||
]
|
||
])
|
||
|
||
// ---- main:预设对话处理 + 冒泡台词/遇水事件走普通风格
|
||
patch('main/index.ts', [
|
||
[
|
||
"import { ITEMS, itemById, levelOf } from '../shared/petState'",
|
||
"import { ITEMS, itemById, levelOf } from '../shared/petState'\nimport { barkText } from '../shared/barks'\nimport { findPreset } from '../shared/dialogue'"
|
||
],
|
||
// 遇水事件文案按风格
|
||
[
|
||
`say('(惊慌)桌上怎么有水?!呜哇,快帮我擦干!(>﹏<)', 6000)`,
|
||
`say(
|
||
currentFlavor() === 'normal'
|
||
? '(惊慌)桌上怎么会有水?!呜哇,快帮我擦干!(>﹏<)'
|
||
: '(惊慌)桌上怎么有水?!呜哇,快帮我擦干!(>﹏<)',
|
||
6000
|
||
)`
|
||
],
|
||
// 预设对话 IPC(搭在 chat 处理后面)
|
||
[
|
||
` ipcMain.handle('pet:bubble:current', () => currentBubble)`,
|
||
` ipcMain.handle('pet:chat:preset', (_e, id: string) => {
|
||
const preset = findPreset(String(id))
|
||
if (!preset) return { ok: false, message: '没有这个预设' }
|
||
const snap = store.snapshot()
|
||
const s = snap.stats
|
||
const score = (s.mood + s.electron + s.satiety + s.paraffin) / 4
|
||
const pGood =
|
||
preset.kind === 'random'
|
||
? 0.5
|
||
: Math.max(0.15, Math.min(0.85, 0.35 + (score - 50) / 125))
|
||
const good = Math.random() < pGood
|
||
store.applyChatEffect(good ? preset.good : preset.bad)
|
||
const reply = good ? preset.replyGood : preset.replyBad
|
||
bubbleStreamStart()
|
||
bubbleStreamChunk(reply)
|
||
bubbleStreamEnd(reply)
|
||
return { ok: true, message: (good ? '🎲 ' : '💧 ') + (good ? preset.good.toast : preset.bad.toast), reply }
|
||
})
|
||
ipcMain.handle('pet:bubble:current', () => currentBubble)`
|
||
]
|
||
])
|
||
|
||
// preload / api:chatPreset
|
||
patch('preload/index.ts', [
|
||
[
|
||
" importIcon: () => Promise<string>",
|
||
" importIcon: () => Promise<string>\n chatPreset: (id: string) => Promise<{ ok: boolean; message: string; reply?: string }>"
|
||
],
|
||
[
|
||
" importIcon: () => ipcRenderer.invoke('pet:icon:import'),",
|
||
" importIcon: () => ipcRenderer.invoke('pet:icon:import'),\n chatPreset: (id: string) => ipcRenderer.invoke('pet:chat:preset', id),"
|
||
]
|
||
])
|
||
patch('renderer/src/api.ts', [
|
||
[
|
||
" importIcon: () => Promise<string>",
|
||
" importIcon: () => Promise<string>\n chatPreset: (id: string) => Promise<{ ok: boolean; message: string; reply?: string }>"
|
||
]
|
||
])
|
||
|
||
console.log('chat presets + flavor barks patched')
|