Files
SodiumPet/tools/patch-stream.cjs

122 lines
3.4 KiB
JavaScript

const fs = require('fs')
const p = 'C:/Users/ASUS/Documents/deepseek-harness/default-workspace/desktop-pet/src/main/index.ts'
let c = fs.readFileSync(p, 'utf8')
function rep(from, to) {
if (!c.includes(from)) {
console.log('MISS ::', from.slice(0, 80).replace(/\n/g, '\\n'))
return
}
c = c.replace(from, to)
}
// 1) 气泡定位改为动态高度(底边固定在宠物头顶上方 10px)
rep(
`function positionBubble(): void {
if (!mainWindow || !bubbleWindow) return
const b = mainWindow.getBounds()
bubbleWindow.setBounds({
x: Math.round(b.x + (b.width - 320) / 2),
y: b.y - 86,
width: 320,
height: 96
})
}`,
`function positionBubble(): void {
if (!mainWindow || !bubbleWindow) return
const b = mainWindow.getBounds()
const h = bubbleWindow.getBounds().height
bubbleWindow.setBounds({
x: Math.round(b.x + (b.width - 320) / 2),
y: b.y - (h - 10),
width: 320,
height: h
})
}`
)
// 2) say 恢复固定高度
rep(
`function say(text: string, duration = 6500): void {
createBubbleWindow()
positionBubble()`,
`function say(text: string, duration = 6500): void {
createBubbleWindow()
bubbleWindow?.setBounds({ width: 320, height: 96 })
positionBubble()`
)
// 3) 流式气泡函数
rep(
`let nextBarkAt = Date.now() + 25_000`,
`let bubbleStreamLen = 0
function bubbleStreamStart(): void {
createBubbleWindow()
bubbleStreamLen = 0
if (bubbleTimer) clearTimeout(bubbleTimer)
bubbleWindow?.setBounds({ width: 320, height: 96 })
positionBubble()
bubbleWindow?.showInactive()
bubbleWindow?.webContents.send('pet:bubble:stream:start')
}
function bubbleStreamChunk(chunk: string): void {
if (!bubbleWindow) return
bubbleStreamLen += chunk.length
const lines = Math.ceil(bubbleStreamLen / 22)
const h = Math.min(300, Math.max(96, 44 + lines * 26))
const b = mainWindow?.getBounds()
if (b) {
bubbleWindow.setBounds({
x: Math.round(b.x + (b.width - 320) / 2),
y: b.y - (h - 10),
width: 320,
height: h
})
}
bubbleWindow.webContents.send('pet:bubble:stream:chunk', { text: chunk })
}
function bubbleStreamEnd(full: string): void {
const duration = Math.min(20_000, 2500 + full.length * 90)
bubbleWindow?.webContents.send('pet:bubble:stream:end', { duration })
if (bubbleTimer) clearTimeout(bubbleTimer)
bubbleTimer = setTimeout(() => bubbleWindow?.hide(), duration)
}
let nextBarkAt = Date.now() + 25_000`
)
// 4) chat IPC 改为流式
rep(
` const eff = chatEffect(t)
if (eff) {
store.applyChatEffect(eff)
mainWindow?.webContents.send('pet:toast', eff.toast)
}
const reply = await chatService.ask(t)
say(reply, Math.min(18_000, 2500 + reply.length * 90))
return { ok: true, reply, effect: eff?.toast ?? '' }`,
` const eff = chatEffect(t)
if (eff) {
store.applyChatEffect(eff)
mainWindow?.webContents.send('pet:toast', eff.toast)
}
bubbleStreamStart()
try {
const reply = await chatService.askStream(t, (chunk) => bubbleStreamChunk(chunk))
bubbleStreamEnd(reply)
return { ok: true, reply, effect: eff?.toast ?? '' }
} catch (err) {
console.error('[llm] 流式失败', err)
const fallback = '(缩了缩)网络好像不太通……等我一下下 (・_・;)'
bubbleStreamChunk(fallback)
bubbleStreamEnd(fallback)
return { ok: false, reply: fallback }
}`
)
fs.writeFileSync(p, c, 'utf8')
console.log('main patched')