Files

83 lines
2.3 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, 70).replace(/\n/g, '\\n'))
return
}
c = c.replace(from, to)
}
// 流式消息缓冲(气泡窗口首次创建时还没加载完,直接 send 会丢)
rep(
`let bubbleReady = false
let pendingBubble: { text: string; duration: number } | null = null`,
`let bubbleReady = false
let pendingBubble: { text: string; duration: number } | null = null
let streamBuffer: Array<{ channel: string; payload: unknown }> | null = null`
)
rep(
` bubbleWindow.webContents.on('did-finish-load', () => {
bubbleReady = true
if (pendingBubble) {
bubbleWindow?.webContents.send('pet:bubble', pendingBubble)
pendingBubble = null
}
})`,
` bubbleWindow.webContents.on('did-finish-load', () => {
bubbleReady = true
if (pendingBubble) {
bubbleWindow?.webContents.send('pet:bubble', pendingBubble)
pendingBubble = null
}
if (streamBuffer) {
for (const item of streamBuffer) {
bubbleWindow?.webContents.send(item.channel, item.payload)
}
streamBuffer = null
}
})`
)
rep(
`function bubbleStreamStart(): void {
createBubbleWindow()
bubbleStreamLen = 0
if (bubbleTimer) clearTimeout(bubbleTimer)
bubbleWindow?.setBounds({ width: 440, height: 96 })
positionBubble()
bubbleWindow?.showInactive()
bubbleWindow?.webContents.send('pet:bubble:stream:start')
}`,
`function sendBubbleStream(channel: string, payload?: unknown): void {
if (!bubbleWindow) return
if (bubbleReady) {
bubbleWindow.webContents.send(channel, payload)
} else {
if (!streamBuffer) streamBuffer = []
streamBuffer.push({ channel, payload })
}
}
function bubbleStreamStart(): void {
createBubbleWindow()
bubbleStreamLen = 0
if (bubbleTimer) clearTimeout(bubbleTimer)
bubbleWindow?.setBounds({ width: 440, height: 96 })
positionBubble()
bubbleWindow?.showInactive()
sendBubbleStream('pet:bubble:stream:start')
}`
)
rep(
` bubbleWindow.webContents.send('pet:bubble:stream:chunk', { text: chunk })`,
` sendBubbleStream('pet:bubble:stream:chunk', { text: chunk })`
)
fs.writeFileSync(p, c, 'utf8')
console.log('bubble stream buffer patched')